views:

317

answers:

4

I am wondering if it is possible to load a .net DLL at runtime, view the methods available and execute one at runtime.

If this is possible could you point me in the right direction

+3  A: 

You need to use Reflection.

You can call Assembly.LoadFile to load a .DLL containing a .Net assembly, then call the GetTypes method on the returned Assembly object to look at the classes in the DLL.
Once you've found a Type object for the class you're interested in, you can call its InvokeMember method to call a function.

Beware that reflection can be quite slow.

SLaks
You can improve the performance of invoking a method via reflection by using `Delegate.CreateDelegate(...)` instead of getting the method and calling it purely by reflection: http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx
Dan Herbert
@Dan: True. However, that's only possible if you know the signature at compile time.
SLaks
@SLaks - or use DynamicMethod.
codekaizen
+5  A: 

Generally, you use System.Reflection classes to do this task.

Specifically, you'd load the DLL via Assembly.Load (or Assembly.LoadFrom) and then call Assembly.GetTypes and then for each type call Type.GetMethods. When you have a MethodInfo, you can call MethodInfo.Invoke on it.

codekaizen
+1  A: 

I found this at reflection eamples

 // get all public static methods of MyClass type
  MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public |
                                                  BindingFlags.Static);


[C#]
// dynamically load assembly from file Test.dll
 Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

[C#]
// get type of class Calculator from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");

[C#]
// create instance of class Calculator
object calcInstance = Activator.CreateInstance(calcType);

[C#]
// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

[C#]
// get value of property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);

[C#]
// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);

[C#]
// get info about static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");

[C#]
// get value of static property: public static double Pi
double piValue = (double)piPropertyInfo.GetValue(null, null);

[C#]
// invoke public instance method: public void Clear()
calcType.InvokeMember("Clear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, null);

[C#]
// invoke private instance method: private void DoClear()
calcType.InvokeMember("DoClear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);

[C#]
// invoke public instance method: public double Add(double number)
double value = (double)calcType.InvokeMember("Add",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, new object[] { 20.0 });

[C#]
// invoke public static method: public static double GetPi()
double piValue = (double)calcType.InvokeMember("GetPi",
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, null);

[C#]
// get value of private field: private double _number
double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
fishhead
+2  A: 

Yes, this is possible, you just start with loading your dll:

Assembly assembly = Assembly.LoadFrom(assemblyPath);

And then to invoke a method inside your dll you'll have to use reflection.

object obj = assembly.CreateInstance(<type.FullName>);

where type.FullName is the FullName property of some type in that assembly.

Once you got your instance, you can invoke your method like this:

MethodInfo methodInfo = obj.GetMethod("MyMethod");
methodInfo.Invoke(obj,null);
Mez