Beispiel #1
0
        /// <summary>
        /// 获得插件对象
        /// </summary>
        /// <param name="pluginCol">当前插件集合</param>
        /// <param name="_type">插件类型</param>
        private static void getPluginObject(PluginCollection pluginCol, Type _type)
        {
            IPlugin plugin = null;

            try
            {
                //object aa = Activator.CreateInstance(_type);
                //创建一个插件对象实例
                plugin = Activator.CreateInstance(_type) as IPlugin;
            }
            catch
            {
                if (AppLog.log.IsErrorEnabled)
                {
                    AppLog.log.Error(_type.FullName + "反射生成对象时发生异常");
                }
            }
            finally
            {
                if (plugin != null)
                {
                    //判断该插件是否已经存在插件集合中了,如果不是则加入该对象
                    if (!pluginCol.Contains(plugin))
                    {
                        pluginCol.Add(plugin);
                    }
                }
            }
        }
 /// <summary>
 /// 添加插件集合到当前的集合
 /// </summary>
 /// <param name="value"></param>
 private void AddRange(PluginCollection value)
 {
     for (int i = 0; i < value.Capacity; i++)
     {
         this.Add((IPlugin)value.List[i]);
     }
 }
 /// <summary>
 /// 解析插件集合中所有的对象
 /// 将其分别装入ICommand,ITool,IToolBarDef,IMenuDefI和DockableWindowDef 5个集合
 /// </summary>
 /// <param name="pluginCol">插件集合</param>
 public void GetPluginArray(PluginCollection pluginCol)
 {
     //遍历插件集合
     foreach (IPlugin plugin in pluginCol)
     {
         //获得Command接口并添加到Command集合中
         MyPluginEngine.ICommand cmd = plugin as MyPluginEngine.ICommand;
         if (cmd != null)
         {
             this._Commands.Add(cmd.ToString(), cmd);
             //找出该Command的Category,如果它还没有添加到Category则添加
             if (cmd.Category != null && _CommandCategory.Contains(cmd.Category) == false)
             {
                 _CommandCategory.Add(cmd.Category);
             }
             continue;
         }
         //获得ITool接口并添加到ITool集合中
         MyPluginEngine.ITool tool = plugin as MyPluginEngine.ITool;
         if (tool != null)
         {
             this._Tools.Add(tool.ToString(), tool);
             if (tool.Category != null && _CommandCategory.Contains(tool.Category) == false)
             {
                 _CommandCategory.Add(tool.Category);
             }
             continue;
         }
         //获得IToolBarDef接口并添加到IToolBarDef集合中
         MyPluginEngine.IToolBarDef toolbardef = plugin as MyPluginEngine.IToolBarDef;
         if (toolbardef != null)
         {
             this._ToolBars.Add(toolbardef.ToString(), toolbardef);
             continue;
         }
         //获得IMenuDef接口并添加到IMenuDef集合中
         MyPluginEngine.IMenuDef menudef = plugin as MyPluginEngine.IMenuDef;
         if (menudef != null)
         {
             this._Menus.Add(menudef.ToString(), menudef);
             continue;
         }
         //获得IDockableWindowDef接口并添加到IDockableWindowDef集合中
         MyPluginEngine.IDockableWindowDef dockablewindowdef = plugin as MyPluginEngine.IDockableWindowDef;
         if (dockablewindowdef != null)
         {
             this._DockableWindows.Add(dockablewindowdef.ToString(), dockablewindowdef);
             continue;
         }
     }
 }
Beispiel #4
0
 /// <summary>
 /// 插件容器迭代器构造函数
 /// </summary>
 /// <param name="mappings">插件集合</param>
 public PluginCollectionEnumerator(PluginCollection mappings)
 {
     _temp       = (IEnumerable)mappings;
     _enumerator = _temp.GetEnumerator();
 }
Beispiel #5
0
        /// <summary>
        /// 从DLL中获得插件对象并加入到插件容器
        /// </summary>
        /// <returns></returns>
        public static PluginCollection GetPluginsFromDll()
        {
            //存储插件的容器
            PluginCollection _PluginCol = new PluginCollection();

            //判断是否存在该文件夹,如果不存在则自动创建一个,避免异常
            if (!Directory.Exists(pluginFolder))
            {
                Directory.CreateDirectory(pluginFolder);
                if (AppLog.log.IsDebugEnabled)
                {
                    AppLog.log.Debug("plugin文件夹不存在,系统自带创建一个!");
                }
            }
            //获得插件文件夹中的每一个dll文件
            string[] _files = Directory.GetFiles(pluginFolder, "*.dll");
            foreach (string _file in _files)
            {
                //根据程序集文件名加载程序集
                Assembly _assembly = Assembly.LoadFrom(_file);
                if (_assembly != null)
                {
                    Type[] _types = null;
                    try
                    {
                        //获得程序集中定义的类型
                        _types = _assembly.GetTypes();
                    }
                    catch
                    {
                        if (AppLog.log.IsErrorEnabled)
                        {
                            AppLog.log.Error("反射类型加载异常!");
                        }
                    }
                    finally
                    {
                        foreach (Type _type in _types)
                        {
                            //获得一个类型所有实现的接口
                            Type[] _interfaces = _type.GetInterfaces();
                            //遍历接口类型
                            foreach (Type theInterface in _interfaces)
                            {
                                //如果满足某种类型,则添加到插件集合对象中
                                switch (theInterface.FullName)
                                {
                                //MyPluginEngine.
                                case "MyPluginEngine.ICommand":
                                case "MyPluginEngine.ITool":
                                case "MyPluginEngine.IMenuDef":
                                case "MyPluginEngine.IToolBarDef":
                                case "MyPluginEngine.IDockableWindowDef":
                                    getPluginObject(_PluginCol, _type);
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return(_PluginCol);
        }
 public PluginCollection(PluginCollection value)
 {
     this.AddRange(value);
 }