Ejemplo n.º 1
0
        public bool LoadPlugin(string pluginName)
        {
            string  AssemblyFile;
            Boolean result = false;

            config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
            PluginConfigurationElement pe = new PluginConfigurationElement();

            String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "";

            try
            {
                for (Int32 i = 0; i < config.PluginCollection.Count; i++)
                {
                    pe = config.PluginCollection[i];
                    if (pe.Name == pluginName)
                    {
                        if (pe.Version == "")
                        {
                            AssemblyFile = path + "\\" + pe.Assembly;
                        }
                        else
                        {
                            AssemblyFile = path + "\\Plugin\\" + pe.Version + "\\" + pe.Assembly;
                            if (!File.Exists(AssemblyFile))
                            {
                                AssemblyFile = path + "\\" + pe.Assembly;
                            }
                        }

                        Assembly      assembly = Assembly.LoadFile(AssemblyFile);
                        Type          type     = assembly.GetType(pe.Type);
                        IServerPlugin instance = (IServerPlugin)Activator.CreateInstance(type);
                        instance.ServerApplication = application;
                        instance.Load();
                        plugins[pluginName] = instance;
                        result = true;

                        AppPluginInfo _apinfo = new AppPluginInfo();
                        _apinfo.Descript        = pe.Description;
                        _apinfo.PluginType      = pe.Type;
                        _apinfo.Name            = pe.Name;
                        _apinfo.FileName        = pe.Assembly;
                        _apinfo.AssemblyVersion = assembly.GetName().Version.ToString();
                        CurrentPluginInfos.Add(_apinfo);
                        break;
                    }
                }
                if (!result)
                {
                    application.WriteMessage(string.Format("未找到插件{0}!", pluginName), EventLogEntryType.Error);
                }
            }
            catch (Exception e)
            {
                application.WriteMessage(string.Format("出现错误:{0}", e.Message), EventLogEntryType.Error);
                result = false;
            }
            return(result);
        }
Ejemplo n.º 2
0
        public bool Contains(string pluginName)
        {
            config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
            PluginConfigurationElement pe = new PluginConfigurationElement();
            List <String> ps = new List <string>();

            for (Int32 i = 0; i < config.PluginCollection.Count; i++)
            {
                pe = config.PluginCollection[i];
                ps.Add(pe.Name);
            }
            return(ps.Contains(pluginName));
        }
Ejemplo n.º 3
0
        public string[] GetAllPluginNames()
        {
            config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
            PluginConfigurationElement pe = new PluginConfigurationElement();
            ArrayList ps = new ArrayList();

            for (Int32 i = 0; i < config.PluginCollection.Count; i++)
            {
                pe = config.PluginCollection[i];
                ps.Add(pe.Name);
            }
            return((String[])ps.ToArray(typeof(String)));
        }
Ejemplo n.º 4
0
        public void LoadAllPlugin()
        {
            application.WriteMessage("开始加载插件!", EventLogEntryType.Information);
            CurrentPluginInfos = new List <AppPluginInfo>();
            PluginConfigurationElement pe = new PluginConfigurationElement();

            config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
            if (config == null)
            {
                application.WriteMessage("未配置插件文件!", EventLogEntryType.Error);
                return;
            }
            String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "";

            try
            {
                for (Int32 i = 0; i < config.PluginCollection.Count; i++)
                {
                    pe = config.PluginCollection[i];
                    if (File.Exists(path + "\\" + pe.Assembly))
                    {
                        Assembly assembly = Assembly.LoadFile(path + "\\" + pe.Assembly);

                        Type          type     = assembly.GetType(pe.Type);
                        IServerPlugin instance = (IServerPlugin)Activator.CreateInstance(type);
                        instance.ServerApplication = application;
                        instance.Load();
                        plugins[pe.Name] = instance;

                        AppPluginInfo _apinfo = new AppPluginInfo();
                        _apinfo.Descript        = pe.Description;
                        _apinfo.PluginType      = pe.Type;
                        _apinfo.Name            = pe.Name;
                        _apinfo.FileName        = pe.Assembly;
                        _apinfo.AssemblyVersion = assembly.GetName().Version.ToString();
                        CurrentPluginInfos.Add(_apinfo);
                    }
                    else
                    {
                        application.WriteMessage(string.Format("服务器端插件文件{0}不存在!", pe.Assembly), EventLogEntryType.Error);
                    }
                }
            }
            catch (Exception e)
            {
                application.WriteMessage(string.Format("出现错误:{0}", e.Message), EventLogEntryType.Error);
            }
            application.WriteMessage("加载插件结束...", EventLogEntryType.Information);
        }