using System.Globalization; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json.Linq; namespace Tnb.Vengine; public class CodeHelper { /// /// 从当前目录往上查找解决方案sln文件所在的目录 /// public static string? GetSolutionDirectoryPath(bool includeParent = false) { var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); while (Directory.GetParent(currentDirectory.FullName) != null) { currentDirectory = Directory.GetParent(currentDirectory.FullName); if (currentDirectory == null) return null; if (Directory.GetFiles(currentDirectory!.FullName).FirstOrDefault(f => f.EndsWith(".sln")) != null) { if (includeParent && currentDirectory.Parent != null) return currentDirectory.Parent.FullName; else return currentDirectory.FullName; } } return null; } /// /// 保存代码到文件 /// public static void SaveCodeToFile(string code, string saveTo) { var dir = Path.GetDirectoryName(saveTo); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir!); } code = System.Web.HttpUtility.HtmlDecode(code); File.WriteAllText(saveTo, code); } /// /// MD5加密 /// /// /// public static string String2MD5(string szValue) { string pwd = string.Empty; MD5 md5 = MD5.Create(); byte[] byt = md5.ComputeHash(Encoding.Unicode.GetBytes(szValue)); for (int i = 0; i < byt.Length; i++) { pwd += byt[i].ToString("x"); //16进制 } char[] arr = pwd.ToCharArray(); Array.Reverse(arr); return new string(arr); } /// /// newtonsoft json 转小驼峰 /// /// /// public static string ToCamelCase(string s) { if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0])) { return s; } char[] array = s.ToCharArray(); for (int i = 0; i < array.Length && (i != 1 || char.IsUpper(array[i])); i++) { bool flag = i + 1 < array.Length; if (i > 0 && flag && !char.IsUpper(array[i + 1])) { if (char.IsSeparator(array[i + 1])) { array[i] = char.ToLower(array[i], CultureInfo.InvariantCulture); } break; } array[i] = char.ToLower(array[i], CultureInfo.InvariantCulture); } return new string(array); } public static string LengthToString(int strLength) { var l = "DbConsts."; if (strLength <= 0) { l += nameof(DbConsts.LengthText); } else if (strLength <= DbConsts.LengthXXS) { l += nameof(DbConsts.LengthXXS); } else if (strLength <= DbConsts.LengthXS) { l += nameof(DbConsts.LengthXS); } else if (strLength <= DbConsts.LengthS) { l += nameof(DbConsts.LengthS); } else if (strLength <= DbConsts.LengthM) { l += nameof(DbConsts.LengthM); } else if (strLength <= DbConsts.LengthL) { l += nameof(DbConsts.LengthL); } else if (strLength <= DbConsts.LengthXL) { l += nameof(DbConsts.LengthXL); } else if (strLength <= DbConsts.LengthXXL) { l += nameof(DbConsts.LengthXXL); } else if (strLength <= DbConsts.LengthXXXL) { l += nameof(DbConsts.LengthXXXL); } else { l += nameof(DbConsts.LengthXXXXL); } return l; } public static int PropDefineToLength(string? propDefine) { float.TryParse(propDefine, out float f); return (int)f; } public static int PropDefineToScale(string? propDefine) { float.TryParse(propDefine, out float f); return (int)(f * 10) % 10; } }