C# 插件编写

//加载插件
private void LoadPlugins()
{
  string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "addons");

  //搜索该目录下的所有的程序集文件,这里就只搜索*.dll文件
  string[] dlls = Directory.GetFiles(path, "*.dll");

  //动态加载每个dll 文件
  foreach (string dllPath in dlls)
  {
    //加载每个程序集
    Assembly assembly = Assembly.LoadFile(dllPath);

    //需要一个接口来进行约定
    Type typeIEditor = typeof(IEditor);

    //获取当前加载的程序集中的所有的public类型
    Type[] types = assembly.GetExportedTypes();

    //遍历判断哪个类型实现了该接口
    foreach (Type userType in types)
    {
      //判断这个类型必须是实现了IEditor接口的类型,并且该类型必须不是抽象的。
      if (typeIEditor.IsAssignableFrom(userType) && !userType.IsAbstract)
      {
        IEditor plugin = (IEditor)Activator.CreateInstance(userType);

        //把插件名称加载到菜单栏上
        //Add()方法的返回值就是刚刚增加的菜单项
        ToolStripItem tsi = tsmiFormat.DropDownItems.Add(plugin.Name);
        //为该菜单项增加一个单击事件
        tsi.Click += new EventHandler(tsi_Click);
        tsi.Tag = plugin;
      }
    }
  }

}

原文地址:https://www.cnblogs.com/bruce1992/p/14040090.html