Reputation: 799
I'm using a SOAP API that has issues: there are a bunch of services in different namespaces called A, B and C. To make a connection and use the API I need to use an authentication object with my password, let's call it AutObj. This AutObj is the same for A B and C but I cannot use the same one because every namespaces have their type. So right now I'm doing this:
class FactoryAut {
public A.AutObj GetAutA (string pw)
A.AutObj AutObj = new A.AutObj();
AutObj.pw = pw;
return AutObj;
}
public B.AutObj GetAutB (string pw)
B.AutObj AutObj = new B.AutObj();
AutObj.pw = pw;
return AutObj;
}
public C.AutObj GetAutC (string pw)
C.AutObj AutObj = new C.AutObj();
AutObj.pw = pw;
return AutObj;
}
I'm thinking about implementing something like this:
public T GetAut (string pw, T)
T.AutObj AutObj = new T.AutObj();
AutObj.pw = pw;
return AutObj;
}
Where I pass to the method what type of object I need. I think I have to use Reflection right? But I don't know how to do that, and I don't know if there is a better solution.
Upvotes: 1
Views: 180
Reputation: 961
In case anyone else is having this issue I'll share. We have the same situation, the same object in different namespaces generated from different wsdls.
We got around it using dynamic
which is imperfect but is another option and a step up from using object.
Upvotes: 0
Reputation: 799
I solved the problem using reflection:
public static class Factory
{
public static T GetAut<T>()
{
T autPar = Activator.CreateInstance<T>();
System.Reflection.FieldInfo[] fi = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var field in fi)
{
switch (field.Name)
{
case "pwField":
field.SetValue(autPar, ConfigurationService.pw);
break;
}
}
return autPar;
}
Upvotes: 0
Reputation: 1063403
Firstly, it would be worth looking at whether your tooling allows re-use of types. WCF does, for example (not that I'm the worlds greatest WCF fan) - which would make this entire issue just disappear.
The next thing I would look at is whether the tooling is generating partial
classes. If it is, you could do something like:
public interface IAutObj
{
string pw {get;set;}
}
namespace A
{
partial class AutObj : IAutObj {}
}
namespace B
{
partial class AutObj : IAutObj {}
}
namespace C
{
partial class AutObj : IAutObj {}
}
These partial
declarations are combined with the code in the generated files (typically .designer.cs
), and will use implicit interface implementation to satisfy IAutObj
.
Finally, you can then have:
public T GetAut<T>(string pw) where T : class, new(), IAutObj
{
var obj = new T();
obj.pw = pw;
return obj;
}
and use:
var autObj = GetAut<A>("abc");
You might even be able to limit the downstream code to the interface rather than T
, i.e.
IAutObj autObj = GetAut<A>("abc");
Upvotes: 2