/// <summary>
        /// 扫描目录内所有 Program 插件;
        /// </summary>
        /// <param name="ProgramDirectory">插件目录</param>
        /// <param name="Extension">文件扩展名</param>
        /// <returns></returns>
        public static IEnumerable <ProgramTemplateClass> ScanProgramPlugins(string ProgramDirectory, string Extension = ".dll")
        {
            LogController.Debug("扫描 Program 插件 : {0}", ProgramDirectory);
            if (!Directory.Exists(ProgramDirectory))
            {
                yield break;
            }

            foreach (string ProgramPath in Directory.GetFiles(ProgramDirectory).Where(ProgramPath => ProgramPath.ToLower().EndsWith(Extension)))
            {
                LogController.Debug("发现 Program 插件 dll 文件 : {0}", ProgramPath);
                Assembly ProgramAssembly = AssemblyController <ProgramTemplateClass> .CreateAssembly(ProgramPath);

                if (ProgramAssembly == null)
                {
                    continue;
                }

                foreach (ProgramTemplateClass ProgramInstance in AssemblyController <ProgramTemplateClass> .CreateTypeInstance(ProgramAssembly))
                {
                    LogController.Info("创建 Program 实例 : {0} ({1})", ProgramInstance.Name, ProgramInstance.Description);
                    yield return(ProgramInstance);
                }
            }
        }
        /// <summary>
        /// 扫描目录内所有 StartUp 插件;
        /// </summary>
        /// <param name="StartUpDirectory">插件目录</param>
        /// <param name="Extension">文件扩展名</param>
        /// <returns></returns>
        public static IEnumerable <StartUpTemplateClass> ScanStartUpPlugins(string StartUpDirectory, string Extension = ".dll")
        {
            LogController.Debug("扫描 StartUp 插件 : {0}", StartUpDirectory);
            if (!Directory.Exists(StartUpDirectory))
            {
                yield break;
            }

            foreach (string StartUpPath in Directory.GetFiles(StartUpDirectory).Where(StartUpPath => StartUpPath.ToLower().EndsWith(Extension)))
            {
                LogController.Debug("发现 StartUp 插件 dll 文件 : {0}", StartUpPath);
                Assembly StartUpAssembly = AssemblyController <StartUpTemplateClass> .CreateAssembly(StartUpPath);

                if (StartUpAssembly == null)
                {
                    continue;
                }

                foreach (StartUpTemplateClass StartUpInstance in AssemblyController <StartUpTemplateClass> .CreateTypeInstance(StartUpAssembly))
                {
                    LogController.Info("创建 StartUp 实例 : {0} ({1})", StartUpInstance.Name, StartUpInstance.Description);
                    yield return(StartUpInstance);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 扫描目录内所有 Logon 插件;
        /// </summary>
        /// <param name="LogonDirectory">插件目录</param>
        /// <param name="Extension">文件扩展名</param>
        /// <returns></returns>
        public static IEnumerable <LogonTemplateClass> ScanLogonPlugins(string LogonDirectory, string Extension = ".dll")
        {
            LogController.Debug("扫描 Logon 插件 : {0}", LogonDirectory);
            if (!Directory.Exists(LogonDirectory))
            {
                yield break;
            }

            foreach (string LogonPath in Directory.GetFiles(LogonDirectory).Where(LogonPath => LogonPath.ToLower().EndsWith(Extension)))
            {
                LogController.Debug("发现 Logon 插件 dll 文件 : {0}", LogonPath);
                Assembly LogonAssembly = AssemblyController <LogonTemplateClass> .CreateAssembly(LogonPath);

                if (LogonAssembly == null)
                {
                    continue;
                }

                foreach (LogonTemplateClass LogonInstance in AssemblyController <LogonTemplateClass> .CreateTypeInstance(LogonAssembly))
                {
                    LogController.Info("创建 Logon 实例 : {0} ({1})", LogonInstance.Name, LogonInstance.Description);
                    yield return(LogonInstance);
                }
            }
        }
        /// <summary>
        /// 从程序集创建指定父类派生的所有子类的实例
        /// </summary>
        /// <param name="PluginAssembly">程序集</param>
        /// <param name="TargetTypeName"></param>
        /// <returns>所有子类的实例</returns>
        public static IEnumerable <T> CreateTypeInstance(Assembly PluginAssembly, string TargetTypeName = "")
        {
            if (PluginAssembly == null)
            {
                yield break;
            }

            LogController.Info("在程序集 {0} 中创建所有 {1} 派生的子类实例 ...", PluginAssembly.FullName, typeof(T).ToString());
            // 仅加载范式类型派生的子类
            foreach (Type PluginType in PluginAssembly.GetTypes().Where(
                         (PluginType =>
                          TargetTypeName == string.Empty ?
                          PluginType.IsSubclassOf(typeof(T)) :
                          PluginType.IsSubclassOf(typeof(T)) && PluginType.Name == TargetTypeName)
                         ))
            {
                LogController.Debug("可加载的插件类型 : {0}", PluginType.FullName);
                //创建允许加载类型的实例
                T PluginInstance = null;
                try
                {
                    PluginInstance = PluginAssembly.CreateInstance(PluginType.FullName) as T;
                }
                catch (Exception ex)
                {
                    LogController.Error("创建 {0} 类型实例遇到异常 : {1}", PluginType.FullName, ex.Message);
                    continue;
                }
                yield return(PluginInstance);
            }
        }
        /// <summary>
        /// 从指定 Program 插件文件获取 Name 的插件实例
        /// </summary>
        /// <param name="ProgramPath">插件路径</param>
        /// <param name="ProgramName">插件名称</param>
        /// <returns></returns>
        public static ProgramTemplateClass GetProgramPlugin(string ProgramPath, string ProgramName)
        {
            LogController.Info("获取 {0} 内的 Program 插件,ProgramName : {1}", ProgramPath, ProgramName);

            Assembly ProgramAssembly = AssemblyController <ProgramTemplateClass> .CreateAssembly(ProgramPath);

            if (ProgramAssembly == null)
            {
                return(null);
            }

            return(AssemblyController <ProgramTemplateClass> .CreateTypeInstance(ProgramAssembly, ProgramName).FirstOrDefault());
        }
        /// <summary>
        /// 从指定 StartUp 插件文件获取 Name 的插件实例
        /// </summary>
        /// <param name="StartUpPath">插件路径</param>
        /// <param name="StartUpName">插件名称</param>
        /// <returns></returns>
        public static StartUpTemplateClass GetStartUpPlugin(string StartUpPath, string StartUpName)
        {
            LogController.Info("获取 {0} 内的 StartUp 插件,StartUpName : {1}", StartUpPath, StartUpName);

            Assembly StartUpAssembly = AssemblyController <StartUpTemplateClass> .CreateAssembly(StartUpPath);

            if (StartUpAssembly == null)
            {
                return(null);
            }

            return(AssemblyController <StartUpTemplateClass> .CreateTypeInstance(StartUpAssembly, StartUpName).FirstOrDefault());
        }
Esempio n. 7
0
        /// <summary>
        /// 从指定 Logon 插件文件获取 Name 的插件实例
        /// </summary>
        /// <param name="LogonPath">插件路径</param>
        /// <param name="LogonName">插件名称</param>
        /// <returns></returns>
        public static LogonTemplateClass GetLogonPlugin(string LogonPath, string LogonName)
        {
            LogController.Info("获取 {0} 内的 Logon 插件,LogonName : {1}", LogonPath, LogonName);

            Assembly LogonAssembly = AssemblyController <LogonTemplateClass> .CreateAssembly(LogonPath);

            if (LogonAssembly == null)
            {
                return(null);
            }

            return(AssemblyController <LogonTemplateClass> .CreateTypeInstance(LogonAssembly, LogonName).FirstOrDefault());
        }
        /// <summary>
        /// 加载程序集
        /// </summary>
        /// <param name="AssemblyPath">程序集路径</param>
        /// <param name="DynamicLoad">在内存中动态加载</param>
        /// <returns>程序集</returns>
        public static Assembly CreateAssembly(string AssemblyPath, bool DynamicLoad = true)
        {
            LogController.Info("开始{0}加载程序集路径:{1} ...", (DynamicLoad ? "动态" : string.Empty), AssemblyPath);

            Assembly PluginAssembly = null;

            try
            {
                if (!DynamicLoad)
                {
                    // 从链接库文件路径加载
                    PluginAssembly = Assembly.LoadFrom(AssemblyPath);
                }
                else
                {
                    // 把链接库文件读入内存后从内存加载,允许程序在运行时更新链接库
                    using (FileStream AssemblyStream = new FileStream(AssemblyPath, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader AssemblyReader = new BinaryReader(AssemblyStream))
                        {
                            byte[] AssemblyBuffer = AssemblyReader.ReadBytes((int)AssemblyStream.Length);
                            PluginAssembly = Assembly.Load(AssemblyBuffer);
                            AssemblyReader.Close();
                        }
                        AssemblyStream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                LogController.Error("创建程序集遇到异常:{0}", ex.Message);
                return(null);
            }

            return(PluginAssembly);
        }