在运行时调用方法

时间:2010-01-31 18:43:09

标签: c# runtime invocation

我想知道是否可以在运行时加载.net DLL,查看可用的方法并在运行时执行一个。

如果有可能,你可以指出我正确的方向

4 个答案:

答案 0 :(得分:7)

通常,您使用System.Reflection类来执行此任务。

具体来说,您可以通过Assembly.Load(或Assembly.LoadFrom)加载DLL,然后调用Assembly.GetTypes,然后调用每种类型的调用Type.GetMethods。如果您有MethodInfo,则可以在其上调用MethodInfo.Invoke

答案 1 :(得分:3)

您需要使用Reflection

您可以调用Assembly.LoadFile来加载包含.Net程序集的.DLL,然后在返回的GetTypes对象上调用Assembly方法来查看DLL中的类。登记/> 找到您感兴趣的课程的Type对象后,您可以调用其InvokeMember方法来调用函数。

注意反射可能很慢。

答案 2 :(得分:3)

是的,这是可能的,你只需要开始加载你的dll:

Assembly assembly = Assembly.LoadFrom(assemblyPath);

然后要在你的dll中调用一个方法,你必须使用reflection

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

其中type.FullName是该程序集中某种类型的FullName属性。

获得实例后,您可以调用这样的方法:

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

答案 3 :(得分:1)

我发现了这个 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);