日期:2014-05-18  浏览次数:20986 次

二次开发,加载DLL,模块化,前两天问了下模块化,现在做了个例子请大家指教
窗口应用程序 命名为Main

窗口加载事件
C# code

private void Main_Load(object sender, EventArgs e)
        {
            //当前程序集路径
            string dir = Assembly.GetEntryAssembly().Location;
            //string dir =Application.ExecutablePath;
            dir = Path.GetDirectoryName(dir);
            if (!Directory.Exists(dir))
            {
                MessageBox.Show("读取DLL失败!");
                return;
            }
            //扫描下所有的dll文件
            string[] dlls = Directory.GetFiles(dir, "*.dll");
            foreach (string item in dlls)
            {
                Assembly asm = Assembly.LoadFile(item);
                Type[] typeArray = asm.GetExportedTypes();
                foreach (Type typeitem in typeArray)
                {
                    Type typeIExtension = typeof(I_Extension.I_Extension);
                    //必须是实现了I_Extension接口,并且不能是抽象类
                    if (typeIExtension.IsAssignableFrom(typeitem) && !typeitem.IsAbstract)
                    {
                        I_Extension.I_Extension IExtension = (I_Extension.I_Extension)Activator.CreateInstance(typeitem);
                        IExtension.Function(this);
                    }
                }
            }
        }



接口类项目I_Extension
C# code

    public interface I_Extension
    {
        /// <summary>
        /// 插件名称
        /// </summary>
        string Name { set; get; }

        /// <summary>
        /// 插件执行内容
        /// </summary>
        void Function(System.Windows.Forms.Form form);
    }



插件DLL项目
C# code

    public class MenuStrip : I_Extension.I_Extension
    {
        private const string SqlCon = "server=.;database=GT_Menu;uid=sa;pwd=000110";
        private string name = "菜单控件";
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public void Function(Form form)
        {
            InitializeComponent(form);
            Bind();
        }

        private System.Windows.Forms.MenuStrip menuStrip1;
        private void InitializeComponent(Form form)
        {
            menuStrip1 = new System.Windows.Forms.MenuStrip();
            // 
            // menuStrip1
            // 
            menuStrip1.Name = "menuStrip1";
            menuStrip1.TabIndex = 0;
            menuStrip1.Text = "menuStrip1";
            //
            //
            //
            form.MainMenuStrip = menuStrip1;
            form.Controls.Add(menuStrip1);
        }

        private void Bind()
        {
            DataTable dt = SqlHelper.ExecuteDataset(SqlCon, CommandType.Text, "select * from Menu_DLL", null).Tables[0];
            DataRow[] dr0 =  dt.Select("[MenuItmeID] = 0");
            for (int i = 0; i < dr0.Length; i++)
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(dr0[i]["MenuItmeDLL"].ToString());
                D_MenuID(dt, Convert.ToInt32(dr0[i]["MenuID"].ToString()), ref tsmi);
                menuStrip1.Items.Add(tsmi);
            }
        }

        void D_MenuID(DataTable dt, int MenuItmeNum, ref ToolStripMenuItem tsmi)
        {
            foreach (DataRow dr in dt.Select("[MenuItmeID] = " + MenuItmeNum.ToString()))
            {
                ToolStripMenuItem tsmitem = new ToolStripMenuItem(dr["MenuItmeDLL"].ToString());
                tsmitem.Tag = dr["MenuID"].ToString();