通过反射加载DLL部分更新应用程序(winform)

来源:互联网 发布:腾讯数据恢复留言 编辑:程序博客网 时间:2024/05/18 00:29

方法:加载dll,再界面创建控件,注册控件的点击事件 

namespace WindowsFormsApplication2  
{  
     
      
    public partial class Form1 :Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            Type[] typePublic = null; Type typeIExecute = null;  
            string exeDirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); //得到运行目录           
             
            string[] dellPaths = Directory.GetFiles(exeDirPath, "*.dll");            
            for (int i = 0; i < dellPaths.Length; i++)  
            {                
                Assembly asm = Assembly.LoadFile(dellPaths[i]);  
  
                //Assembly asm = Assembly.LoadFrom("ClassLibrary1.dll");   
                typePublic = asm.GetExportedTypes();//得到所有的public方法  
                typeIExecute = typeof(IExecute);//接口的类型  
  
                for (int j = 0; j < typePublic.Length; j++)  
                {  
                    if (typeIExecute.IsAssignableFrom(typePublic[j]) && !typePublic[j].IsAbstract)//实现了IExecute,且非抽象类  
                    {  
                        IExecute execut = (IExecute)Activator.CreateInstance(typePublic[j]);  
                        ToolStripItem tsoitem = menuStrip1.Items.Add(execut.ExeName);//注册菜单  
                        tsoitem.Click += new EventHandler(tsoitem_Click);//添加点击事件  
                        tsoitem.Tag = execut;//将namespace.class记录在新增控件的tag属性中  
                    }  
                }  
            }  
             
              
        }  
        void tsoitem_Click(object sender, EventArgs e)  
        {  
             
            IExecute execut = (IExecute)((ToolStripItem)sender).Tag;  
  
            execut.Execute();  
        }  
        
    }  
  
    public interface IExecute  
    {  
         
        string ExeName  
        {  
            get;  
            set;  
        }  
  
        ///<summary>  
        ///插件执行的方法  
        ///</summary>  
        void Execute();  
    }  
}  



system.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using WindowsFormsApplication2;  
[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
namespace ClassLibrary1  
{  
    public class One : IExecute  
    {  
        public One()  
        {  
            ExeName = "MenuOne";  
        }  
       public string ExeName  
        {  
            get;  
            set;  
        }  
  
  
  
        public void Execute()  
        {  
            MessageBox.Show("Execute One");  
        }  
    }  
}  


第二个DLL
[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using WindowsFormsApplication2;  
using System.Windows.Forms;  
  
namespace ClassLibrary2  
{  
   public class Two:IExecute  
    {  
        public Two()  
        {  
            ExeName = "MenuTwo";  
        }  
       public string ExeName  
        {  
            get;  
            set;  
        }  
  
  
  
        public void Execute()  
        {  
            MessageBox.Show("Execute ClassTwo");  
        }  
    }  
}  

0 0
原创粉丝点击