自己做个笔记:
List<object> plugins=new List<object>{}; //存放插件的列表
string path=DLL文件夹路径;
string[] files=所有的DLL数组;
foreach(string dllFile in files) //遍历每一个DLL
{
Assembly ably=Assembly.LodeFile(dllFile);
Type[] types=ably.GetTypes();
foreach(Type t in types )
{
if(t.GetInterface("接口名称")!=null) //此类继承了指定的接口
{
Button btn=new Button();
plugins.add(ably.CreateInterface(t.FullName));
btn.Tag=t.FullName;
//添加 btn 到指定窗体
}
bingBtnEvents(); // 为 btn 绑定事件
}
}
private void bingBtnEvents()
{
string fullName=btn.Tag.toString();
foreach(var v in plugins)
{
object obj =v as object;
Type t=obj.GetType();
if(t.FullName==fuleName)
{
MethodInfo info=t.GetMetgod("方法名称");
info.Invoke(obj,null);
}
}
}