加载程序集,实现编辑器插件(反射)

来源:互联网 发布:代理服务器ip和端口 编辑:程序博客网 时间:2024/05/01 19:04
 1.插件实现机制,动态加载DLL,遍历DLL文件,调用无参构造函数创建对象。
 private void Form1_Load(object sender, EventArgs e)        {            string path = @"D:\dll";            string[] files=Directory.GetFiles(path,"*.dll");            foreach (string file in files)            {                Assembly asm = Assembly.LoadFile(file);                Type[] types = asm.GetExportedTypes();                Type type = types[0];                object obj = Activator.CreateInstance(type);                Plugwhight pw = obj as Plugwhight;                ToolStripMenuItem mi1 = new ToolStripMenuItem(pw.Name);                工具ToolStripMenuItem.DropDownItems.Add(mi1);                mi1.Click += new EventHandler(mi1_Click);                mi1.Tag = obj;                                      }        }        void mi1_Click(object sender, EventArgs e)        {                      ToolStripMenuItem mi = sender as ToolStripMenuItem;            Plugwhight pw = (Plugwhight)mi.Tag;            textBox1.Text = pw.ReplText(textBox1.Text);        }


接口

public interface Plugwhight
    {
        string Name { get; }
        string ReplText(string oldText);
    }

原创粉丝点击