# 个人应用库使用说明 ## 命令行运行CmdRunner ``` // 直接运行命令 static void Execute(string command) // 运行命令并获取返回值 string ExecuteWithOutput(string command) ``` ## 一个枚举类Enumeration ``` public class CardType : Enumeration { // 下面这种是字段,用Enumeration.GetAllFields()获取所有字段 public static CardType Amex = new(1, nameof(Amex)); // 下面两种是属性,用Enumeration.GetAllProperties()获取所有属性 public static CardType Amex => new(1, nameof(Amex)); public static CardType Amex { get; } = new(1, nameof(Amex)); // 无论那种方式,public static都是必须的修饰符,缺少就会发生错误 public CardType(int id, string name) : base(id, name){} } ``` ## 雪花算法IdWorker ``` IdWorker work = new IdWorker(2, 1); long id = work.NextId(); ``` ## 消息内容Notice 私有化构造函数,只能用下列两种方法创建 ``` //标准方法 static Notice Create(NoticeType type, object? description = null) //用字符串快速构建 static Notice Create(string? msg) ``` ## 单例模式泛型基类Singleton ``` public class Demo : Singleton { //派生类必须有且仅有一个无参private构造函数 private Demo(){} } //使用举例 Demo.Instance.SomeMethod(); ``` ## 简单的模版引擎类Template ``` //对应的模板文件中,变量使用{{和}}包括起来 string val = TemplateA.Create("模板内容").Set("变量去掉花括号","实际内容").Render(); ``` ## 字符串扩展 ``` // 把一个字符串数组、List等用指定分隔符(缺省是半角逗号)连接成一个字符串 string Join(this IEnumerable strs, string separate = ",") // 检测字符串中是否包含列表中的关键词 bool Contains(this string? s, IEnumerable keys, bool ignoreCase = true) // 用给定的分隔符拆分源字符串,并查找指定键值是否存在 bool ContainsKey(this string? source, string? key, params char[] separations) // 取字符串前length个字符 string Take(this string? s, int length) // 简单验证邮箱地址合法性 bool IsEmail(this string? email) // 获取指定字符串的拼音首字母(全部小写) string GetPinYinFirstLetter(this string? source, params char[] ignoreChars) // 将一个字符串转换成最简单Notice的扩展 Notice ToNotice(this string? content) ``` ## 枚举扩展 ``` // 获取Enum项对应的int值 int ToInt(this System.Enum e) ``` ## 对称加解密AESHelper ``` // 把一个字符串加密成字符串 string Encrypt(string key, string data) // 把一个字符串加密成byte数组 byte[] EncryptStringToBytes(string key, string data) // 由给定的string密文解密 string Decrypt(string key, string encryptData) // 由给定的byte数组密文解密 string Decrypt(string key, byte[] inputBytes) // 生成指定长度的随机字符串 string GetRandomStr(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword) ``` ## 非对称加解密RSAHelper ``` // 使用公钥加密 string Encrypt(string text, string publicKey) // 私钥解密 string Decrypt(string encryptText, string privateKey) // 获取公钥和私钥 Dictionary GetKey() ``` ## 简单的Log输出帮助类LogHelper ``` // 输出DEBUG信息,另外还有Info和Error,大同小异 void Debug(string content) ``` ## 简单的网络操作NetHelper ``` //从指定网址下载一个文件 int DownloadFile(string remoteFile, string localFile) // 检查网络连通性 bool CheckInternetConnection(string hosturl, int timeout = 1000) ``` ## 文本处理类StrHelper ``` // 用MessageBox.Show弹出窗口显示一些信息 void ShowMessage(string? msg, bool isSuccess = true) // 用自定义的遮罩窗口弹出显示一些信息 MessageBoxResult ShowMaskMessage(string message, string caption = "提示信息", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage image = MessageBoxImage.Information) // 用File.WriteAllText导出字符串到一个文件 int ExportFile(string fileName, string content) // 打开Microsoft.Win32.OpenFileDialog并获得文件名 string OpenDialogGetFileName(string filter = "CSV Files|*.csv", string defaultExt = ".csv") // 打开Microsoft.Win32.OpenFileDialog并获得文件名的字符串数组 string[] OpenDialogGetFileNames(string filter = "All Files|*.*", string defaultExt = "") // 压缩一个文件夹到一个指定的文件夹的某个文件中 string ZipDirectory(string sourePath, string archivedPath, string? archivedFileName = null) // 压缩一个或几个文件到一个文件 bool ZipFiles(string[] sourceFiles, string archivedFile) ``` ## XML序列化、反序列化XmlHelper ``` // 序列化到字符串 string SerializeToXmlString(T xmlObject, bool useNamespaces = true) // 序列化到文件 void SerializeToXmlFile(T xmlObject, string filename, bool useNamespaces = true) // 从字符串反向序列化 T DeserializeFromXmlString(string xml) where T : new() // 从文件反向序列化 T DeserializeFromXmlFile(string filename) where T : new() ``` ## 集成MaterialDesignThemes 在`App.xaml`中,把空白`Application.Resources`修改成如下内容 ``` ``` ```html ``` ## 集成ExcelDataReader ## 集成EPPlus ### 规范Excel导出一些基本的配置信息的抽象基类ExcelExportBaseConfig 1. 缺省:字体为11号的微软雅黑;标题、副标题、头部均为Null;每次导出均重建文件 实际应用中必须新建新类来继承此类,最简单的可以不做如何更改 ``` abstract class ExcelExportBaseConfig : IExcelConfig ``` 2. 操作Excel的帮助类ExcelHelper ``` /// /// 将一个List导出到Excel文件 /// 使用ISKExcelConfig接口进行详细配置 /// 可包括标题、副标题、头部标识,也可全部为空 /// /// 需要导出List具体的Model类型 /// TModel类型的List /// 具体配置类的实例,需实现ISKExcelConfig接口 /// 导出完成之后,再做一些附加操作 void ExportListToExcel(List models, IExcelConfig config, Action? action = null) ```
鲁ICP备17027395号-1