Q
在运行时调用方法
9
A
回答
7
通常,您使用System.Reflection类来完成此任务。
具体来说,您可以通过Assembly.Load(或Assembly.LoadFrom)加载DLL,然后调用Assembly.GetTypes,然后为每个类型调用Type.GetMethods。当您有MethodInfo时,您可以拨打MethodInfo.Invoke。
3
您需要使用Reflection。
您可以拨打Assembly.LoadFile
加载包含.Net程序集的.DLL,然后在返回的Assembly
对象上调用GetTypes
方法来查看DLL中的类。
一旦你找到了你感兴趣的类的Type
对象,你可以调用它的InvokeMember
方法来调用一个函数。
请注意,反射可能会很慢。
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);
相关问题
- 1. 在运行时调用方法
- 2. 异步运行时方法调用
- 3. 运行时未调用的打印方法,调试时调用
- 4. 在运行时调用方法的各种方法是什么?
- 5. Toast在调用方法之前运行
- 6. 使用在运行时解析的类型调用System.Linq.Queryable方法
- 7. 在C#运行时使用反射调用ToList()方法#
- 8. 正在调用应用程序仍在运行时调用UITableViewDelegate dealloc方法
- 9. 调度java方法在onbiz的特定时间运行使用
- 10. 如何找到在运行时调用Ruby方法的位置?
- 11. PHP如何在运行时从类中调用方法?
- 12. 如何在调用时才运行方法?
- 13. JavaFX线程问题 - 在方法调用运行时GUI冻结
- 14. 仅在调用时才运行方法(JavaScript)
- 15. 在Rails中运行另一种方法之前调用方法
- 16. 主题:未调用运行方法
- 17. 为什么不运行方法调用?
- 18. 调度方法在ASP.NET运行
- 19. BeginReceiveFrom仅在运行数据包嗅探器时调用的回调方法
- 20. 仅在调试模式下运行时才从我的TestMethod调用方法
- 21. 是运行时方法
- 22. 运行方法同时
- 23. PHP:运行另一个类方法时运行类方法?
- 24. .net仲裁运行时类实例和方法调用
- 25. 运行python mox test时永远不会调用预期方法
- 26. 通过Python/SUDS调用运行时SOAP方法
- 27. 设置symfony2运行时服务的方法调用
- 28. Silverlight 4.0异步调用长时间运行方法(MVVM)
- 29. 运行程序时调用日志方法
- 30. c#运行时程序集装入,ByRef方法调用
可以提高通过反射调用方法的性能:
一旦你得到了你的情况,你可以调用你的方法是这样该方法并纯粹通过反射来调用它:http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx – 2010-01-31 18:52:10
@Dan:True。但是,这只有在编译时知道签名的情况下才有可能。 – SLaks 2010-01-31 18:53:57
或使用DynamicMethod。 – codekaizen 2010-01-31 19:11:04