2010-01-31 80 views
9

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

如果这是可能的,你可以点我在正确的方向

回答

3

您需要使用Reflection

您可以拨打Assembly.LoadFile加载包含.Net程序集的.DLL,然后在返回的Assembly对象上调用GetTypes方法来查看DLL中的类。
一旦你找到了你感兴趣的类的Type对象,你可以调用它的InvokeMember方法来调用一个函数。

请注意,反射可能会很慢。

+0

可以提高通过反射调用方法的性能:

一旦你得到了你的情况,你可以调用你的方法是这样该方法并纯粹通过反射来调用它:http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx – 2010-01-31 18:52:10

+1

@Dan:True。但是,这只有在编译时知道签名的情况下才有可能。 – SLaks 2010-01-31 18:53:57

+0

或使用DynamicMethod。 – codekaizen 2010-01-31 19:11:04

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); 
3

是的,这是可能的,你刚开始加载您的DLL:

Assembly assembly = Assembly.LoadFrom(assemblyPath); 

然后调用你的DLL,你会里面的方法必须使用reflection

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

其中type.FullName是该程序集中某种类型的FullName属性。通过使用`Delegate.CreateDelegate(...)`,而不是领

MethodInfo methodInfo = obj.GetMethod("MyMethod"); 
methodInfo.Invoke(obj,null); 
相关问题