zoukankan      html  css  js  c++  java
  • 动态加载DLL(C#)

     public class PlugingManager
        {
            //插件装载器
            public ArrayList Plugins = new ArrayList();
            //插件FullName
            public ArrayList PlugFullName = new ArrayList();
            //插件类型
            public ArrayList PlugTypes = new ArrayList();

            #region 构造函数
            /// <summary>
            
    /// PlugingManager插件加载
            
    /// </summary>
            
    /// <param name="plugspath">插件所在目录必须是运行目录中的文件夹</param>
            
    /// <param name="StartsWith">加载指定插件(插件包含的名称)</param>
            
    /// <param name="InterfaceName">插件接口名称</param>
            public PlugingManager(string plugspath,string StartsWith,string InterfaceName)
            {
                //获取插件目录(plugins)下所有文件
                string[] files = Directory.GetFiles(Application.StartupPath + @"\\" + plugspath);
               
                foreach (string file in files)
                {
                    if (file.ToUpper().EndsWith(StartsWith.ToUpper()))
                    {
                        try
                        {
                            //Assembly ab = Assembly.LoadFrom(file);
                            Assembly ab = null;

                            //先将插件拷贝到内存缓冲
                            byte[] addinStream = null;
                            using(FileStream input = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                            {
                                BinaryReader reader = new BinaryReader(input);
                                addinStream = reader.ReadBytes((int) input.Length);

                            }
                            ab = Assembly.Load(addinStream); //加载内存中的Dll
                            Type[] types = ab.GetTypes();
                            foreach (Type t in types)
                            {
                                if (t.GetInterface(InterfaceName) != null)
                                {
                                    Plugins.Add(ab.CreateInstance(t.FullName));
                                    PlugFullName.Add(t.FullName);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            #endregion
        }
      PlugingManager plug = new PlugingManager("Plugs""Garden.Plugs.dll""IPlug");

                var win = plug.Plugins.ToArray().FirstOrDefault(m => ((Type)m.GetType()).Name.ToLower() == this.Tag.ToString().ToLower());
                MethodInfo OnShowDlg = ((Type)win.GetType()).GetMethod("ShowSelf");
                Form cl = (Form)OnShowDlg.Invoke(win, null);
                cl.WindowState = FormWindowState.Maximized;
                cl.MdiParent = this;
                cl.Show();
                foreach (object obj in plug.Plugins)
                {

                    Type t = obj.GetType();
                    MethodInfo OnShowDlg = t.GetMethod("ShowSelf");
                    Control cl = (Control)OnShowDlg.Invoke(obj, null);

                   Control con = GetControlFromForm(t.Name, this);
                    if (con != null)
                    {
                        con.Controls.Add(cl);
                        cl.Dock = DockStyle.Fill;
                        isbreak = false;
                        con = null;
                    }

                } 

  • 相关阅读:
    HTML5游戏引擎Phaser初体验
    React+Node.js+Express+mongoskin+MongoDB
    React的一个简单示例
    在线白板,基于socket.io的多人在线协作工具
    使用node-webkit开发exe窗口程序
    使用Node.js的socket.io模块开发实时web程序
    dota BP练习工具开发:一个C/S多用户程序
    SQL注入之延迟盲注
    CTFHUB之gopher协议实现SSRF
    python的蟒蛇绘制
  • 原文地址:https://www.cnblogs.com/keepsilence/p/2465923.html
Copyright © 2011-2022 走看看