extern alias dll1;
extern alias dll2;
...
public void DoStuff1(){
dll1::NameSpace.Class.Method();
}
public void DoStuff2(){
dll2::NameSpace.Class.Method();
}
What I'd like to be able to do is:
public void DoStuff(alias a){
a::NameSpace.Class.Method();
}
alias does not appear to be usable like this.
Addendum: dll1 and dll2 are different versions of the same dll.
Keep code from different aliases in different files, and use partial classes in each file. (Partial classes allow you to write code for the same class in different files.) That should do it.
For instance:
File 1:
extern alias dll1;
partial class foo
{
public void DoStuff1(){
dll1::NameSpace.Class.Method();
}
}
File 2:
extern alias dll2;
partial class foo
{
public void DoStuff2(){
dll2::NameSpace.Class.Method();
}
}
Here is some reflection code. It is based on code given in O'Reilly's C# Cookbook by Hilyard and Teilhet. I adapted it and removed some stuff, and I haven't tested it, but if you use reflection, something like this would work for you. You would just have to pass in the correct assembly name (still haven't figured out how to deal with dlls with same namespace, but there has to be a way), and the correct class and method names, and this function will call the appropriate method for you.
A problem with reflection is that you lose some type checking, since you have to work with generic objects, and you have to use strings to represent namespaces, types, and members.
object InvokeMethod(string assembly, string type, string method, object[] parameters)
{
Assembly asm = Assembly.LoadFrom(assembly);
Type classtype = asm.GetType(type, true, false);
object dynamicObject = Activator.CreateInstance(classtype);
MethodInfo invokedMethod = classtype.GetMethod(method);
return invokedMethod.Invoke(dynamicObject,parameters);
}
I ended up using dynamic in .Net 4.0.
The combination of using external aliases with dynamic was the cleanest and simplest solution for this problem.
Can you make an enum which will specify which alias to use? Then you'd have something like
public void DoStuff(int a){
if(a==0)
dll1:Namespace.Class.Method()
else if(a==1)
dll2:Namespace.Class.Method()
}