/// <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插件集合 /// </summary> private void LoadProgram() { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { foreach (ProgramTemplateClass ProgramInstance in ProgramController.ScanProgramPlugins(UnityModule.ProgramDirectory)) { try { ProgramIconControl ProgramCard = new ProgramIconControl(ProgramInstance.Name, ProgramInstance.Icon, ProgramInstance); ProgramCard.Click += new EventHandler( (s, e) => { ProgramTemplateClass CurrentProgram = (s as ProgramIconControl).ParentProgram; CurrentProgram.GetNewProgramForm().Show(this); }); this.Invoke(new Action(delegate { ProgramLayoutPanel.Controls.Add(ProgramCard); })); } catch (Exception ex) { LogController.Error("加载程序插件遇到异常:{0}", ex.Message); } } })); }
/// <summary> /// 转换图像为 Base64 编码 /// </summary> /// <param name="ImageObject">图像</param> /// <param name="ImageFormatType">图像格式</param> /// <returns>Base64</returns> public static string ImageToBase64(Image ImageObject, ImageFormat ImageFormatType) { LogController.Debug("转换图像(HashCode = 0x{0})为 Base64 编码 ...", ImageObject.GetHashCode().ToString("X")); try { MemoryStream ImageStream = new MemoryStream(); ImageObject.Save(ImageStream, ImageFormatType); return(Convert.ToBase64String(ImageStream.GetBuffer())); } catch (Exception ex) { LogController.Error("转换图像为 Base64 编码时遇到错误:{0}", ex.Message); return(string.Empty); } }
/// <summary> /// 转换 Base64 编码为图像 /// </summary> /// <param name="Base64String">Base64</param> /// <returns>图像</returns> public static Image Base64ToImage(string Base64String) { LogController.Debug("转换 Base64 编码 (HashCode = 0x{0})为图像 ...", Base64String.GetHashCode().ToString("X")); try { byte[] ImageData = Convert.FromBase64String(Base64String); MemoryStream ImageStream = new MemoryStream(ImageData); return(Image.FromStream(ImageStream)); } catch (Exception ex) { LogController.Error("转换 Base64 编码为图像时遇到错误:{0}", ex.Message); return(null); } }
/// <summary> /// 复制文件 /// </summary> /// <param name="SourcesPath">源文件路径</param> /// <param name="TargetPath">目标文件路径</param> public static void CopyFile(string SourcesPath, string TargetPath, bool OverWrite, bool ThrowException) { LogController.Debug("复制文件 \"{0}\" 至 \"{1}\",{2}允许复写,{3}允许抛出异常 ", SourcesPath, TargetPath, OverWrite ? "" : "不", ThrowException ? "" : "不"); try { File.Copy(SourcesPath, TargetPath, OverWrite); } catch (Exception CopyException) { LogController.Error("复制文件遇到错误:{0}", CopyException.Message); if (ThrowException) { throw CopyException; } } }
/// <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); }
/// <summary> /// 复制目录 /// </summary> /// <param name="SourceDirectory">源目录</param> /// <param name="TargetDirectory">目标目录</param> /// <param name="CopyChildDir">是否复制子目录</param> /// <returns>返回 (总目录数, 成功数, 总文件数, 成功数)</returns> public static Tuple <int, int, int, int> CopyDirectory(string SourceDirectory, string TargetDirectory, bool CopyChildDir) { LogController.Debug("复制目录:{0} => {1}", SourceDirectory, TargetDirectory); //依次对应返回值 int DirCount = 0, DirOKCount = 0, FileCount = 0, FileOKCount = 0; if (!Directory.Exists(SourceDirectory)) { LogController.Warn("源目录 {0} 不存在,无法复制", SourceDirectory); return(new Tuple <int, int, int, int>(0, 0, 0, 0)); } if (!Directory.Exists(TargetDirectory)) { try { Directory.CreateDirectory(TargetDirectory); } catch (Exception ex) { LogController.Error("创建目录 {0} 失败:{1}", TargetDirectory, ex.Message); return(new Tuple <int, int, int, int>(0, 0, 0, 0)); } } DirOKCount++; //复制文件 foreach (string FilePath in Directory.GetFiles(SourceDirectory)) { LogController.Debug("复制文件:{0}", FilePath); try { File.Copy(FilePath, PathCombine(TargetDirectory, Path.GetFileName(FilePath)), true); FileOKCount++; } catch (Exception ex) { LogController.Error("复制文件遇到错误:{0}", ex.Message); } finally { FileCount++; } } if (CopyChildDir) { //复制子目录 foreach (string ChildDirectoryPath in Directory.GetDirectories(SourceDirectory)) { DirCount++; Tuple <int, int, int, int> CopyResult = CopyDirectory(ChildDirectoryPath, PathCombine(TargetDirectory, Path.GetFileName(ChildDirectoryPath)), CopyChildDir); DirCount += CopyResult.Item1; DirOKCount += CopyResult.Item2; FileCount += CopyResult.Item3; FileOKCount += CopyResult.Item4; LogController.Debug("复制子目录:{0}\n\t\t\t\t\t\t\t DirCount:{1} DirOKCount:{2} FileCount:{3} FileOKCount:{4}", ChildDirectoryPath, CopyResult.Item1, CopyResult.Item2, CopyResult.Item3, CopyResult.Item4); } } DirOKCount++; return(new Tuple <int, int, int, int>(DirCount, DirOKCount, FileCount, FileOKCount)); }