这两天在看自定义控件,原来有太多知识没有掌握。今天看到插件机制,心里突然一亮,这个东西听了不少次,就是不知道是啥回事。这次有幸书里包含一个案例,我就跟着它一步步来。终于知道是什么回事了。这个应该在软件开发中非常多见。只是当时不理解罢了。
开始
新建一个winform项目CustomControls
在窗体上放一个button按钮
窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Plugin;
namespace CustomControls
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnok_Click(object sender, EventArgs e)
        {
            CreatePlugInDockForm("", "Plugin.Form1", "Plugin");
        }
       public static void CreatePlugInDockForm(string strPlugInSubFilePath, string strFormFullName, string strAssemblyName)
        {
            try
            {
                //项目的Assembly选项名称DockSample.dll 即 DockSample
                string path = strAssemblyName;
                //类的全路径名称=> “DockSample.Modules.frm班组日志管理”
                string name = strFormFullName;
                string currentDirectory =
                System.IO.Directory.GetCurrentDirectory();
                    if (strPlugInSubFilePath == "")
                    {
                             currentDirectory = currentDirectory + "\\PlugIns";
                    }
                    else
                    {
                            //同样的dll,可以按照打上客户的名称,然后加载对应客户的dll,这样就可以防止冲突
                            currentDirectory = currentDirectory + "\\PlugIns" + "\\" +
                            strPlugInSubFilePath;
                    }
            string[] dllFilenames = System.IO.Directory.GetFiles(currentDirectory,
            strAssemblyName + ".dll");
                if (dllFilenames.Length == 1)
                {
                    Assembly asm = Assembly.LoadFrom(dllFilenames[0]);
                    Form frm = (Form)asm.CreateInstance(strFormFullName, true);
                    //实现PlugIn.ICMFormPlugIn接口
                    if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
                    {
                        frm.Show();
                    }
                    else
                    {
                       
                        MessageBox.Show("没有实现接口");
                    }
                }
             }
              catch (Exception ex)
                {
                   MessageBox.Show(ex.Message);
     }
     }
   }
  }
 再新建一个Plugin类库项目 
 加一个接口ICMFormPlugIn
 接口代码
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Plugin
{
  public  interface ICMFormPlugIn
    {
        void Show();
        void Close();
    }
}
 在里面再新建一个form1窗体,并继承接口。再拖一个按钮控件
 窗体代码
 
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Plugin
{
    public partial class Form1 : Form,ICMFormPlugIn
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hello wo shi chajian");
        }
    }
}
第一个项目要引用后面插件项目,不然在实现接口调用时错误,这个地方
 //实现PlugIn.ICMFormPlugIn接口
if (frm.GetType().GetInterface(typeof(ICMFormPlugIn).FullName) != null)
其实也可以直接写
frm.GetType().GetInterface("ICMFormPlugIn")
在CustomControls项目bin里新建一个文件夹,把生成的Plugin.dll文件放入其中。然后运行就有一下效果了

算是完成了。
参考《.NET 控件开发基础》
原文:http://www.cnblogs.com/xiaohuasan/p/5667536.html