I have three assemblies, A.dll
, B.dll
, C.dll
that all define a single namespace A, B, C and a single class A, B, C.
The classes in each assembly have the exact same API, identical in every way.
I now need to write an application that uses all three assemblies and I am struggling to find the best way to do this.
For example the classes define the following function:
string GetName();
In my application I have a matching function, but how can I simplify the implementation?
A MyA = new A();
B MyB = null;
C MyC = null;
public string GetName() {
if (MyA != null) return MyA.GetName();
else if (MyB != null) return MyB.GetName();
else if (MyC != null) return MyC.GetName();
}
I have a lot of functions in this API so it would be messy to do this if/else over and over again.
Interfaces
I thought about defining an interface for the API, but I don't want to add another assembly that my application and these three assemblies are dependent on. I also don't want to create an inter-dependency between any of these assemblies. This is because the assemblies are used individually in other situations that unfortunately only allow for a single assembly, which is out of my control.
Reflection
I thought about using reflection and delegates:
private void GetAPI() {
var Api = MyA;
Type t = Api.GetType();
System.Reflection.MethodInfo m = t.GetMethod("GetName");
_GetName = (GetNameDelegate)Delegate.CreateDelegate(typeof(GetNameDelegate), Api, m);
}
public string GetName() {
return _GetName();
}
This works, but how do I expand this to re-use this piece of code for all three assemblies? I.e. how to pass in MyA, MyB, MyC into the GetAPI function?
Thanks!