简体   繁体   English

动态调用DLL中的方法

[英]Dynamically invoke a method in DLL

I have a DLL containing some methods (show, hide and validate). 我有一个包含一些方法(显示,隐藏和验证)的DLL。 Here is an example of one of the methods hide(Panel paneldynamic, String id, List<EventActions> eventList) . 这是方法hide(Panel paneldynamic, String id, List<EventActions> eventList) All methods contains the same parameters. 所有方法都包含相同的参数。

Now I have referenced the my DLL on my main form, how can I dynamically invoke one of the methods at runtime? 现在,我已经在主窗体上引用了DLL,如何在运行时动态调用其中一种方法?

You'll need to use reflection. 您将需要使用反射。 First, load the assembly (note that this assumes you've imported System.Reflection ): 首先,加载程序集(请注意,这假定您已导入System.Reflection ):

Assembly a = Assembly.LoadFile(pathToTheDll);

Get the type containing the method by fully-qualified name: 通过完全限定的名称获取包含方法的类型:

Type t = a.GetType("Some.Class");

Now, get the method: 现在,获取方法:

MethodInfo m = t.GetMethod("hide"); // For example

Then, all you have to do is invoke it: 然后,您要做的就是调用它:

m.Invoke(null, new object[] { /* parameters go here */ });

The first argument to Invoke is the instance. Invoke的第一个参数是实例。 If your class is static , use null , otherwise, you'll need to supply an instance of the type created using Assembly.CreateInstance . 如果您的类是static ,请使用null ,否则,您将需要提供一个使用Assembly.CreateInstance创建的类型的实例。

Insert your methods as delegates into a dictionary 将您的方法作为委托插入字典中

public enum MethodType
{
    None,
    Show,
    Hide,
    Validate
}

var methods = new Dictionary<MethodType, Action<Panel, String, List<EventActions>>();
methods.Add(MethodType.Show, show);
methods.Add(MethodType.Hide, hide);
methods.Add(MethodType.Validate, validate);

Then you can invoke one of them with 然后,您可以使用

MethodType methodToInvoke = MethodType.Hide;
methods[methodToInvoke](paneldynamic, id, eventList);

If you intend to dynamically load the DLL, this is another story. 如果打算动态加载DLL,则这是另一回事了。 You will need at least three assemblies (three projects): one main assembly (exe), one contract assembly (dll) and one plug-in assembly (dll). 您将至少需要三个程序集(三个项目):一个主程序集(exe),一个合同程序集(dll)和一个插件程序(dll)。 The main and the plug-in assembly both have to reference the contract assembly. 主组件和插件组件都必须引用合同组件。 The contract assembly contains an interface 合同程序集包含一个接口

public interface IPlugIn
{
    void Show(Panel paneldynamic, String id, List<EventActions> eventList);
    void Hide(Panel paneldynamic, String id, List<EventActions> eventList);
    void Validate(Panel paneldynamic, String id, List<EventActions> eventList);
}

The plug-in assembly contains a class implementing the interface 插件程序集包含一个实现接口的类

public class PlugIn : IPlugIn
{
    // TODO: implement IPlugIn
}

In the main assembly you can load the plug-in like this 在主组件中,您可以像这样加载插件

IPlugIn LoadPlugInFromFile(string fileName)
{
    Assembly asm = Assembly.LoadFrom(fileName);
    Type type = asm.GetType("PlugIn");
    IPlugIn plugIn = (IPlugIn)Activator.CreateInstance(type);
    return plugIn;
}

Invoke like this 像这样调用

IPlugIn plugIn = LoadPlugInFromFile("C:\PlugIns\MyPlugIn.dll");
plugIn.Show(paneldynamic, id, eventList);

You can get type by Reflection and invoke method like this: 您可以通过反射获取类型并调用如下方法:

type.InvokeMember("hide", BindingFlags.InvokeMethod | BindingFlags.Static |   
BindingFlags.Public, null, null, new object[] { paneldynamic, id, eventList });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM