| | |
| | | using NetTaste; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Text.RegularExpressions; |
| | | using System.Threading.Tasks; |
| | | using System.Web; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.Enums; |
| | | |
| | | namespace WIDESEA_Core.Helper |
| | | { |
| | | public static class UtilConvert |
| | | { |
| | | private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0); |
| | | |
| | | private static long longTime = 621355968000000000; |
| | | |
| | | private static int samllTime = 10000000; |
| | | /// <summary> |
| | | /// 时间戳转换成日期 |
| | | /// </summary> |
| | | /// <param name="timeStamp"></param> |
| | | /// <returns></returns> |
| | | public static DateTime GetTimeSpmpToDate(this object timeStamp) |
| | | { |
| | | if (timeStamp == null) return dateStart; |
| | | DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime(); |
| | | return dateTime; |
| | | } |
| | | |
| | | public static string Serialize(this object obj, JsonSerializerSettings formatDate = null) |
| | | { |
| | | if (obj == null) return null; |
| | | formatDate = formatDate ?? new JsonSerializerSettings |
| | | { |
| | | DateFormatString = "yyyy-MM-dd HH:mm:ss" |
| | | }; |
| | | return JsonConvert.SerializeObject(obj, formatDate); |
| | | } |
| | | |
| | | public static T DeserializeObject<T>(this string json) |
| | | { |
| | | if (string.IsNullOrEmpty(json)) |
| | | { |
| | | return default(T); |
| | | } |
| | | if (json == "{}") |
| | | { |
| | | json = "[]"; |
| | | } |
| | | return JsonConvert.DeserializeObject<T>(json); |
| | | } |
| | | |
| | | public static string FirstLetterToLower(this string thisValue) |
| | | { |
| | | if (string.IsNullOrEmpty(thisValue)) return string.Empty; |
| | | string result = thisValue.Substring(0, 1).ToLower() + thisValue.Substring(1, thisValue.Length - 1); |
| | | return result; |
| | | } |
| | | |
| | | public static string FirstLetterToUpper(this string thisValue) |
| | | { |
| | | if (string.IsNullOrEmpty(thisValue)) return string.Empty; |
| | | string result = thisValue.Substring(0, 1).ToUpper() + thisValue.Substring(1, thisValue.Length - 1); |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static int ObjToInt(this object thisValue) |
| | | { |
| | | int reval = 0; |
| | | if (thisValue == null) return 0; |
| | | if (thisValue is Enum && thisValue != DBNull.Value && Enum.TryParse(thisValue.GetType(), thisValue.ToString(), out var val)) |
| | | { |
| | | return Convert.ToInt32(val.ChangeType(typeof(int))); |
| | | } |
| | | if (thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return reval; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static int DoubleToInt(this double thisValue) |
| | | { |
| | | int reval = 0; |
| | | |
| | | return Convert.ToInt32(thisValue); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <param name="errorValue"></param> |
| | | /// <returns></returns> |
| | | public static int ObjToInt(this object thisValue, int errorValue) |
| | | { |
| | | int reval = 0; |
| | | if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return errorValue; |
| | | } |
| | | |
| | | public static long ObjToLong(this object thisValue) |
| | | { |
| | | long reval = 0; |
| | | if (thisValue == null) return 0; |
| | | if (thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return reval; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static double ObjToMoney(this object thisValue) |
| | | { |
| | | double reval = 0; |
| | | if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <param name="errorValue"></param> |
| | | /// <returns></returns> |
| | | public static double ObjToMoney(this object thisValue, double errorValue) |
| | | { |
| | | double reval = 0; |
| | | if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return errorValue; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static string ObjToString(this object thisValue) |
| | | { |
| | | if (thisValue != null) return thisValue.ToString().Trim(); |
| | | return ""; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static bool IsNotEmptyOrNull(this object thisValue) |
| | | { |
| | | return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null"; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <param name="errorValue"></param> |
| | | /// <returns></returns> |
| | | public static string ObjToString(this object thisValue, string errorValue) |
| | | { |
| | | if (thisValue != null) return thisValue.ToString().Trim(); |
| | | return errorValue; |
| | | } |
| | | |
| | | public static bool IsNullOrEmpty(this object thisValue) => thisValue == null || thisValue == DBNull.Value || string.IsNullOrWhiteSpace(thisValue.ToString()); |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static Decimal ObjToDecimal(this object thisValue) |
| | | { |
| | | Decimal reval = 0; |
| | | if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <param name="errorValue"></param> |
| | | /// <returns></returns> |
| | | public static Decimal ObjToDecimal(this object thisValue, decimal errorValue) |
| | | { |
| | | Decimal reval = 0; |
| | | if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return errorValue; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static DateTime ObjToDate(this object thisValue) |
| | | { |
| | | DateTime reval = DateTime.MinValue; |
| | | if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | reval = Convert.ToDateTime(thisValue); |
| | | } |
| | | |
| | | return reval; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <param name="errorValue"></param> |
| | | /// <returns></returns> |
| | | public static DateTime ObjToDate(this object thisValue, DateTime errorValue) |
| | | { |
| | | DateTime reval = DateTime.MinValue; |
| | | if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return errorValue; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static bool ObjToBool(this object thisValue) |
| | | { |
| | | bool reval = false; |
| | | if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval)) |
| | | { |
| | | return reval; |
| | | } |
| | | |
| | | return reval; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 获取当前时间的时间戳 |
| | | /// </summary> |
| | | /// <param name="thisValue"></param> |
| | | /// <returns></returns> |
| | | public static string DateToTimeStamp(this DateTime thisValue) |
| | | { |
| | | TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0); |
| | | return Convert.ToInt64(ts.TotalSeconds).ToString(); |
| | | } |
| | | |
| | | public static object ChangeType(this object value, Type type) |
| | | { |
| | | if (value == null && type.IsGenericType) return Activator.CreateInstance(type); |
| | | if (value == null) return null; |
| | | if (type == value.GetType()) return value; |
| | | if (type.IsEnum) |
| | | { |
| | | if (value is string) |
| | | return Enum.Parse(type, value as string); |
| | | else |
| | | return Enum.ToObject(type, value); |
| | | } |
| | | |
| | | if (!type.IsInterface && type.IsGenericType) |
| | | { |
| | | Type innerType = type.GetGenericArguments()[0]; |
| | | object innerValue = ChangeType(value, innerType); |
| | | return Activator.CreateInstance(type, new object[] { innerValue }); |
| | | } |
| | | |
| | | if (value is string && type == typeof(Guid)) return new Guid(value as string); |
| | | if (value is string && type == typeof(Version)) return new Version(value as string); |
| | | if (!(value is IConvertible)) return value; |
| | | return Convert.ChangeType(value, type); |
| | | } |
| | | |
| | | public static object ChangeTypeList(this object value, Type type) |
| | | { |
| | | if (value == null) return default; |
| | | |
| | | var gt = typeof(List<>).MakeGenericType(type); |
| | | dynamic lis = Activator.CreateInstance(gt); |
| | | |
| | | var addMethod = gt.GetMethod("Add"); |
| | | string values = value.ToString(); |
| | | if (values != null && values.StartsWith("(") && values.EndsWith(")")) |
| | | { |
| | | string[] splits; |
| | | if (values.Contains("\",\"")) |
| | | { |
| | | splits = values.Remove(values.Length - 2, 2) |
| | | .Remove(0, 2) |
| | | .Split("\",\""); |
| | | } |
| | | else |
| | | { |
| | | splits = values.Remove(0, 1) |
| | | .Remove(values.Length - 2, 1) |
| | | .Split(","); |
| | | } |
| | | |
| | | foreach (var split in splits) |
| | | { |
| | | var str = split; |
| | | if (split.StartsWith("\"") && split.EndsWith("\"")) |
| | | { |
| | | str = split.Remove(0, 1) |
| | | .Remove(split.Length - 2, 1); |
| | | } |
| | | |
| | | addMethod.Invoke(lis, new object[] { ChangeType(str, type) }); |
| | | } |
| | | } |
| | | |
| | | return lis; |
| | | } |
| | | |
| | | public static string ToJson(this object value) |
| | | { |
| | | return JsonConvert.SerializeObject(value); |
| | | } |
| | | |
| | | public static bool IsNumeric(this object value) |
| | | { |
| | | if (value == null) |
| | | return false; |
| | | bool reslut = decimal.TryParse(value.ToString(), out decimal _number); |
| | | return reslut; |
| | | } |
| | | |
| | | public static bool IsInt(this object obj) |
| | | { |
| | | if (obj == null) |
| | | return false; |
| | | bool reslut = Int32.TryParse(obj.ToString(), out int _number); |
| | | return reslut; |
| | | |
| | | } |
| | | public static bool IsDate(this object str) |
| | | { |
| | | return str.IsDate(out _); |
| | | } |
| | | public static bool IsDate(this object str, out DateTime dateTime) |
| | | { |
| | | dateTime = DateTime.Now; |
| | | if (str == null || str.ToString() == "") |
| | | { |
| | | return false; |
| | | } |
| | | return DateTime.TryParse(str.ToString(), out dateTime); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据传入格式判断是否为小数 |
| | | /// </summary> |
| | | /// <param name="str"></param> |
| | | /// <param name="formatString">18,5</param> |
| | | /// <returns></returns> |
| | | public static bool IsNumber(this string str, string formatString) |
| | | { |
| | | if (string.IsNullOrEmpty(str)) return false; |
| | | |
| | | return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"); |
| | | } |
| | | |
| | | public static bool IsGuid(this string guid) |
| | | { |
| | | Guid newId; |
| | | return guid.GetGuid(out newId); |
| | | } |
| | | |
| | | public static bool GetGuid(this string guid, out Guid outId) |
| | | { |
| | | Guid emptyId = Guid.Empty; |
| | | return Guid.TryParse(guid, out outId); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 转换为短整型 qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="data">数据</param> |
| | | public static short ToShort(this object data) |
| | | { |
| | | if (data == null) |
| | | return 0; |
| | | short result; |
| | | var success = short.TryParse(data.ToString(), out result); |
| | | if (success) |
| | | return result; |
| | | try |
| | | { |
| | | return Convert.ToInt16(data); |
| | | } |
| | | catch (Exception) |
| | | { |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 转换为高精度浮点数,并按指定的小数位4舍5入 qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="data">数据</param> |
| | | /// <param name="digits">小数位数</param> |
| | | public static decimal ToDecimal(this object data, int digits) |
| | | { |
| | | return Math.Round(ObjToDecimal(data), digits); |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// 枚举转List qy 2024-7-15 |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <returns></returns> |
| | | public static List<object> GetEnumList<T>() |
| | | { |
| | | List<object> list = new List<object>(); |
| | | var t = typeof(T); |
| | | foreach (var item in Enum.GetValues(typeof(T))) |
| | | { |
| | | FieldInfo f = t.GetField(Enum.GetName(typeof(T), item)); |
| | | var r = (System.ComponentModel.DescriptionAttribute[])f.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); |
| | | list.Add(new { ID = item, value = Enum.GetName(typeof(T), item), name = r[0].Description }); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 验证动态类型元素是否为空 qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="obj"></param> |
| | | /// <returns></returns> |
| | | public static bool CheckDynamic(object obj) |
| | | { |
| | | return (obj != null && Convert.ToString(obj) != "") ? true : false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 动态类型元素转字符串并解码 qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="obj"></param> |
| | | /// <returns></returns> |
| | | public static string DynamicToString(object obj) |
| | | { |
| | | return CheckDynamic(obj) ? HttpUtility.UrlDecode(Convert.ToString(obj).Trim()) : ""; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// DataTable转为json qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="parObjRefDataTable">DataTable</param> |
| | | /// <returns>json数据</returns> |
| | | public static string ToJson(this DataTable parObjRefDataTable) |
| | | { |
| | | List<object> objArrList = new List<object>(); |
| | | |
| | | foreach (DataRow objDataRow in parObjRefDataTable.Rows) |
| | | { |
| | | Dictionary<string, object> objResult = new Dictionary<string, object>(); |
| | | |
| | | foreach (DataColumn objDataColumn in parObjRefDataTable.Columns) |
| | | objResult.Add(objDataColumn.ColumnName, objDataRow[objDataColumn].ToString()); |
| | | |
| | | objArrList.Add(objResult); |
| | | } |
| | | |
| | | return ToJson(objArrList); |
| | | } |
| | | /// <summary> |
| | | /// Json转List qy 2024-7-15 |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="parJson"></param> |
| | | /// <returns></returns> |
| | | public static List<T> JsonToList<T>(this string parJson) |
| | | { |
| | | if (string.IsNullOrWhiteSpace(parJson)) |
| | | { |
| | | return new List<T>(); |
| | | } |
| | | else |
| | | { |
| | | return JsonConvert.DeserializeObject<List<T>>(parJson); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 日期转为unix值 |
| | | /// </summary> |
| | | /// <param name="dt"></param> |
| | | /// <param name="utc">T:按UTC时间计算(默认);F:按本地时间计算</param> |
| | | /// <returns></returns> |
| | | /// <summary> |
| | | /// 日期转为unix值 |
| | | /// </summary> |
| | | /// <param name="dt">日期时间</param> |
| | | /// <param name="utc">T:按UTC时间计算(默认);F:按本地时间计算</param> |
| | | /// <returns></returns> |
| | | public static double ToUnix(this DateTime dt, bool utc = true) |
| | | { |
| | | double intResult = 0; |
| | | System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); |
| | | intResult = (dt - startTime).TotalSeconds; |
| | | intResult = Math.Round(intResult, 0); |
| | | return intResult; |
| | | } |
| | | |
| | | #region 判断是否为字符串 qy 2024-7-15 |
| | | /// <summary> |
| | | /// 判断字符串是否为合法的json字符串 qy 2024-7-15 |
| | | /// </summary> |
| | | /// <param name="json"></param> |
| | | /// <returns></returns> |
| | | public static bool IsJson(string json) |
| | | { |
| | | int errIndex; |
| | | return IsJson(json, out errIndex); |
| | | } |
| | | public static bool IsJson(string json, out int errIndex) |
| | | { |
| | | errIndex = 0; |
| | | if (IsJsonStart(ref json)) |
| | | { |
| | | CharState cs = new CharState(); |
| | | char c; |
| | | for (int i = 0; i < json.Length; i++) |
| | | { |
| | | c = json[i]; |
| | | if (SetCharState(c, ref cs) && cs.childrenStart)//设置关键符号状态。 |
| | | { |
| | | string item = json.Substring(i); |
| | | int err; |
| | | int length = GetValueLength(item, true, out err); |
| | | cs.childrenStart = false; |
| | | if (err > 0) |
| | | { |
| | | errIndex = i + err; |
| | | return false; |
| | | } |
| | | i = i + length - 1; |
| | | } |
| | | if (cs.isError) |
| | | { |
| | | errIndex = i; |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | return !cs.arrayStart && !cs.jsonStart; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private static bool IsJsonStart(ref string json) |
| | | { |
| | | if (!string.IsNullOrEmpty(json)) |
| | | { |
| | | json = json.Trim('\r', '\n', ' '); |
| | | if (json.Length > 1) |
| | | { |
| | | char s = json[0]; |
| | | char e = json[json.Length - 1]; |
| | | return (s == '{' && e == '}') || (s == '[' && e == ']'); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取值的长度(当Json值嵌套以"{"或"["开头时) qy 2024-7-15 |
| | | /// </summary> |
| | | private static int GetValueLength(string json, bool breakOnErr, out int errIndex) |
| | | { |
| | | errIndex = 0; |
| | | int len = 0; |
| | | if (!string.IsNullOrEmpty(json)) |
| | | { |
| | | CharState cs = new CharState(); |
| | | char c; |
| | | for (int i = 0; i < json.Length; i++) |
| | | { |
| | | c = json[i]; |
| | | if (!SetCharState(c, ref cs))//设置关键符号状态。 |
| | | { |
| | | if (!cs.jsonStart && !cs.arrayStart)//json结束,又不是数组,则退出。 |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | else if (cs.childrenStart)//正常字符,值状态下。 |
| | | { |
| | | int length = GetValueLength(json.Substring(i), breakOnErr, out errIndex);//递归子值,返回一个长度。。。 |
| | | cs.childrenStart = false; |
| | | cs.valueStart = 0; |
| | | //cs.state = 0; |
| | | i = i + length - 1; |
| | | } |
| | | if (breakOnErr && cs.isError) |
| | | { |
| | | errIndex = i; |
| | | return i; |
| | | } |
| | | if (!cs.jsonStart && !cs.arrayStart)//记录当前结束位置。 |
| | | { |
| | | len = i + 1;//长度比索引+1 |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return len; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 字符状态 qy 2024-7-15 |
| | | /// </summary> |
| | | private class CharState |
| | | { |
| | | internal bool jsonStart = false;//以 "{"开始了... |
| | | internal bool setDicValue = false;// 可以设置字典值了。 |
| | | internal bool escapeChar = false;//以"\"转义符号开始了 |
| | | /// <summary> |
| | | /// 数组开始【仅第一开头才算】,值嵌套的以【childrenStart】来标识。 |
| | | /// </summary> |
| | | internal bool arrayStart = false;//以"[" 符号开始了 |
| | | internal bool childrenStart = false;//子级嵌套开始了。 |
| | | /// <summary> |
| | | /// 【0 初始状态,或 遇到“,”逗号】;【1 遇到“:”冒号】 |
| | | /// </summary> |
| | | internal int state = 0; |
| | | |
| | | /// <summary> |
| | | /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】 |
| | | /// </summary> |
| | | internal int keyStart = 0; |
| | | /// <summary> |
| | | /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】 |
| | | /// </summary> |
| | | internal int valueStart = 0; |
| | | internal bool isError = false;//是否语法错误。 |
| | | |
| | | internal void CheckIsError(char c)//只当成一级处理(因为GetLength会递归到每一个子项处理) |
| | | { |
| | | if (keyStart > 1 || valueStart > 1) |
| | | { |
| | | return; |
| | | } |
| | | //示例 ["aa",{"bbbb":123,"fff","ddd"}] |
| | | switch (c) |
| | | { |
| | | case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}] |
| | | isError = jsonStart && state == 0;//重复开始错误 同时不是值处理。 |
| | | break; |
| | | case '}': |
| | | isError = !jsonStart || (keyStart != 0 && state == 0);//重复结束错误 或者 提前结束{"aa"}。正常的有{} |
| | | break; |
| | | case '[': |
| | | isError = arrayStart && state == 0;//重复开始错误 |
| | | break; |
| | | case ']': |
| | | isError = !arrayStart || jsonStart;//重复开始错误 或者 Json 未结束 |
| | | break; |
| | | case '"': |
| | | case '\'': |
| | | isError = !(jsonStart || arrayStart); //json 或数组开始。 |
| | | if (!isError) |
| | | { |
| | | //重复开始 [""",{"" "}] |
| | | isError = (state == 0 && keyStart == -1) || (state == 1 && valueStart == -1); |
| | | } |
| | | if (!isError && arrayStart && !jsonStart && c == '\'')//['aa',{}] |
| | | { |
| | | isError = true; |
| | | } |
| | | break; |
| | | case ':': |
| | | isError = !jsonStart || state == 1;//重复出现。 |
| | | break; |
| | | case ',': |
| | | isError = !(jsonStart || arrayStart); //json 或数组开始。 |
| | | if (!isError) |
| | | { |
| | | if (jsonStart) |
| | | { |
| | | isError = state == 0 || (state == 1 && valueStart > 1);//重复出现。 |
| | | } |
| | | else if (arrayStart)//["aa,] [,] [{},{}] |
| | | { |
| | | isError = keyStart == 0 && !setDicValue; |
| | | } |
| | | } |
| | | break; |
| | | case ' ': |
| | | case '\r': |
| | | case '\n'://[ "a",\r\n{} ] |
| | | case '\0': |
| | | case '\t': |
| | | break; |
| | | default: //值开头。。 |
| | | isError = (!jsonStart && !arrayStart) || (state == 0 && keyStart == -1) || (valueStart == -1 && state == 1);// |
| | | break; |
| | | } |
| | | //if (isError) |
| | | //{ |
| | | |
| | | //} |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 设置字符状态(返回true则为关键词,返回false则当为普通字符处理) qy 2024-7-15 |
| | | /// </summary> |
| | | private static bool SetCharState(char c, ref CharState cs) |
| | | { |
| | | cs.CheckIsError(c); |
| | | switch (c) |
| | | { |
| | | case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}] |
| | | #region 大括号 |
| | | if (cs.keyStart <= 0 && cs.valueStart <= 0) |
| | | { |
| | | cs.keyStart = 0; |
| | | cs.valueStart = 0; |
| | | if (cs.jsonStart && cs.state == 1) |
| | | { |
| | | cs.childrenStart = true; |
| | | } |
| | | else |
| | | { |
| | | cs.state = 0; |
| | | } |
| | | cs.jsonStart = true;//开始。 |
| | | return true; |
| | | } |
| | | #endregion |
| | | break; |
| | | case '}': |
| | | #region 大括号结束 |
| | | if (cs.keyStart <= 0 && cs.valueStart < 2 && cs.jsonStart) |
| | | { |
| | | cs.jsonStart = false;//正常结束。 |
| | | cs.state = 0; |
| | | cs.keyStart = 0; |
| | | cs.valueStart = 0; |
| | | cs.setDicValue = true; |
| | | return true; |
| | | } |
| | | // cs.isError = !cs.jsonStart && cs.state == 0; |
| | | #endregion |
| | | break; |
| | | case '[': |
| | | #region 中括号开始 |
| | | if (!cs.jsonStart) |
| | | { |
| | | cs.arrayStart = true; |
| | | return true; |
| | | } |
| | | else if (cs.jsonStart && cs.state == 1) |
| | | { |
| | | cs.childrenStart = true; |
| | | return true; |
| | | } |
| | | #endregion |
| | | break; |
| | | case ']': |
| | | #region 中括号结束 |
| | | if (cs.arrayStart && !cs.jsonStart && cs.keyStart <= 2 && cs.valueStart <= 0)//[{},333]//这样结束。 |
| | | { |
| | | cs.keyStart = 0; |
| | | cs.valueStart = 0; |
| | | cs.arrayStart = false; |
| | | return true; |
| | | } |
| | | #endregion |
| | | break; |
| | | case '"': |
| | | case '\'': |
| | | #region 引号 |
| | | if (cs.jsonStart || cs.arrayStart) |
| | | { |
| | | if (cs.state == 0)//key阶段,有可能是数组["aa",{}] |
| | | { |
| | | if (cs.keyStart <= 0) |
| | | { |
| | | cs.keyStart = (c == '"' ? 3 : 2); |
| | | return true; |
| | | } |
| | | else if ((cs.keyStart == 2 && c == '\'') || (cs.keyStart == 3 && c == '"')) |
| | | { |
| | | if (!cs.escapeChar) |
| | | { |
| | | cs.keyStart = -1; |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | cs.escapeChar = false; |
| | | } |
| | | } |
| | | } |
| | | else if (cs.state == 1 && cs.jsonStart)//值阶段必须是Json开始了。 |
| | | { |
| | | if (cs.valueStart <= 0) |
| | | { |
| | | cs.valueStart = (c == '"' ? 3 : 2); |
| | | return true; |
| | | } |
| | | else if ((cs.valueStart == 2 && c == '\'') || (cs.valueStart == 3 && c == '"')) |
| | | { |
| | | if (!cs.escapeChar) |
| | | { |
| | | cs.valueStart = -1; |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | cs.escapeChar = false; |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | #endregion |
| | | break; |
| | | case ':': |
| | | #region 冒号 |
| | | if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 0) |
| | | { |
| | | if (cs.keyStart == 1) |
| | | { |
| | | cs.keyStart = -1; |
| | | } |
| | | cs.state = 1; |
| | | return true; |
| | | } |
| | | // cs.isError = !cs.jsonStart || (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1); |
| | | #endregion |
| | | break; |
| | | case ',': |
| | | #region 逗号 //["aa",{aa:12,}] |
| | | |
| | | if (cs.jsonStart) |
| | | { |
| | | if (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1) |
| | | { |
| | | cs.state = 0; |
| | | cs.keyStart = 0; |
| | | cs.valueStart = 0; |
| | | //if (cs.valueStart == 1) |
| | | //{ |
| | | // cs.valueStart = 0; |
| | | //} |
| | | cs.setDicValue = true; |
| | | return true; |
| | | } |
| | | } |
| | | else if (cs.arrayStart && cs.keyStart <= 2) |
| | | { |
| | | cs.keyStart = 0; |
| | | //if (cs.keyStart == 1) |
| | | //{ |
| | | // cs.keyStart = -1; |
| | | //} |
| | | return true; |
| | | } |
| | | #endregion |
| | | break; |
| | | case ' ': |
| | | case '\r': |
| | | case '\n'://[ "a",\r\n{} ] |
| | | case '\0': |
| | | case '\t': |
| | | if (cs.keyStart <= 0 && cs.valueStart <= 0) //cs.jsonStart && |
| | | { |
| | | return true;//跳过空格。 |
| | | } |
| | | break; |
| | | default: //值开头。。 |
| | | if (c == '\\') //转义符号 |
| | | { |
| | | if (cs.escapeChar) |
| | | { |
| | | cs.escapeChar = false; |
| | | } |
| | | else |
| | | { |
| | | cs.escapeChar = true; |
| | | return true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | cs.escapeChar = false; |
| | | } |
| | | if (cs.jsonStart || cs.arrayStart) // Json 或数组开始了。 |
| | | { |
| | | if (cs.keyStart <= 0 && cs.state == 0) |
| | | { |
| | | cs.keyStart = 1;//无引号的 |
| | | } |
| | | else if (cs.valueStart <= 0 && cs.state == 1 && cs.jsonStart)//只有Json开始才有值。 |
| | | { |
| | | cs.valueStart = 1;//无引号的 |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | public static LinqExpressionType GetLinqCondition(this string stringType) |
| | | { |
| | | LinqExpressionType linqExpression; |
| | | switch (stringType) |
| | | { |
| | | case HtmlElementType.Contains: |
| | | linqExpression = LinqExpressionType.In; |
| | | break; |
| | | case HtmlElementType.ThanOrEqual: |
| | | linqExpression = LinqExpressionType.ThanOrEqual; |
| | | break; |
| | | case HtmlElementType.LessOrequal: |
| | | linqExpression = LinqExpressionType.LessThanOrEqual; |
| | | break; |
| | | case HtmlElementType.GT: |
| | | linqExpression = LinqExpressionType.GreaterThan; |
| | | break; |
| | | case HtmlElementType.lt: |
| | | linqExpression = LinqExpressionType.LessThan; |
| | | break; |
| | | case HtmlElementType.like: |
| | | linqExpression = LinqExpressionType.Contains; |
| | | using NetTaste;
|
| | | using Newtonsoft.Json;
|
| | | using System;
|
| | | using System.Collections.Generic;
|
| | | using System.Data;
|
| | | using System.Linq;
|
| | | using System.Reflection;
|
| | | using System.Text;
|
| | | using System.Text.RegularExpressions;
|
| | | using System.Threading.Tasks;
|
| | | using System.Web;
|
| | | using WIDESEA_Core.Const;
|
| | | using WIDESEA_Core.Enums;
|
| | |
|
| | | namespace WIDESEA_Core.Helper
|
| | | {
|
| | | public static class UtilConvert
|
| | | {
|
| | | private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
|
| | |
|
| | | private static long longTime = 621355968000000000;
|
| | |
|
| | | private static int samllTime = 10000000;
|
| | | /// <summary>
|
| | | /// 时间戳转换成日期
|
| | | /// </summary>
|
| | | /// <param name="timeStamp"></param>
|
| | | /// <returns></returns>
|
| | | public static DateTime GetTimeSpmpToDate(this object timeStamp)
|
| | | {
|
| | | if (timeStamp == null) return dateStart;
|
| | | DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
|
| | | return dateTime;
|
| | | }
|
| | |
|
| | | public static string Serialize(this object obj, JsonSerializerSettings formatDate = null)
|
| | | {
|
| | | if (obj == null) return null;
|
| | | formatDate = formatDate ?? new JsonSerializerSettings
|
| | | {
|
| | | DateFormatString = "yyyy-MM-dd HH:mm:ss"
|
| | | };
|
| | | return JsonConvert.SerializeObject(obj, formatDate);
|
| | | }
|
| | |
|
| | | public static T DeserializeObject<T>(this string json)
|
| | | {
|
| | | if (string.IsNullOrEmpty(json))
|
| | | {
|
| | | return default(T);
|
| | | }
|
| | | if (json == "{}")
|
| | | {
|
| | | json = "[]";
|
| | | }
|
| | | return JsonConvert.DeserializeObject<T>(json);
|
| | | }
|
| | |
|
| | | public static string FirstLetterToLower(this string thisValue)
|
| | | {
|
| | | if (string.IsNullOrEmpty(thisValue)) return string.Empty;
|
| | | string result = thisValue.Substring(0, 1).ToLower() + thisValue.Substring(1, thisValue.Length - 1);
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static string FirstLetterToUpper(this string thisValue)
|
| | | {
|
| | | if (string.IsNullOrEmpty(thisValue)) return string.Empty;
|
| | | string result = thisValue.Substring(0, 1).ToUpper() + thisValue.Substring(1, thisValue.Length - 1);
|
| | | return result;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static int ObjToInt(this object thisValue)
|
| | | {
|
| | | int reval = 0;
|
| | | if (thisValue == null) return 0;
|
| | | if (thisValue is Enum && thisValue != DBNull.Value && Enum.TryParse(thisValue.GetType(), thisValue.ToString(), out var val))
|
| | | {
|
| | | return Convert.ToInt32(val.ChangeType(typeof(int)));
|
| | | }
|
| | | if (thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return reval;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static int DoubleToInt(this double thisValue)
|
| | | {
|
| | | int reval = 0;
|
| | |
|
| | | return Convert.ToInt32(thisValue);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <param name="errorValue"></param>
|
| | | /// <returns></returns>
|
| | | public static int ObjToInt(this object thisValue, int errorValue)
|
| | | {
|
| | | int reval = 0;
|
| | | if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return errorValue;
|
| | | }
|
| | |
|
| | | public static long ObjToLong(this object thisValue)
|
| | | {
|
| | | long reval = 0;
|
| | | if (thisValue == null) return 0;
|
| | | if (thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return reval;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static double ObjToMoney(this object thisValue)
|
| | | {
|
| | | double reval = 0;
|
| | | if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return 0;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <param name="errorValue"></param>
|
| | | /// <returns></returns>
|
| | | public static double ObjToMoney(this object thisValue, double errorValue)
|
| | | {
|
| | | double reval = 0;
|
| | | if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return errorValue;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static string ObjToString(this object thisValue)
|
| | | {
|
| | | if (thisValue != null) return thisValue.ToString().Trim();
|
| | | return "";
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static bool IsNotEmptyOrNull(this object thisValue)
|
| | | {
|
| | | return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null";
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <param name="errorValue"></param>
|
| | | /// <returns></returns>
|
| | | public static string ObjToString(this object thisValue, string errorValue)
|
| | | {
|
| | | if (thisValue != null) return thisValue.ToString().Trim();
|
| | | return errorValue;
|
| | | }
|
| | |
|
| | | public static bool IsNullOrEmpty(this object thisValue) => thisValue == null || thisValue == DBNull.Value || string.IsNullOrWhiteSpace(thisValue.ToString());
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static Decimal ObjToDecimal(this object thisValue)
|
| | | {
|
| | | Decimal reval = 0;
|
| | | if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return 0;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <param name="errorValue"></param>
|
| | | /// <returns></returns>
|
| | | public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
|
| | | {
|
| | | Decimal reval = 0;
|
| | | if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return errorValue;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static DateTime ObjToDate(this object thisValue)
|
| | | {
|
| | | DateTime reval = DateTime.MinValue;
|
| | | if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | reval = Convert.ToDateTime(thisValue);
|
| | | }
|
| | |
|
| | | return reval;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <param name="errorValue"></param>
|
| | | /// <returns></returns>
|
| | | public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
|
| | | {
|
| | | DateTime reval = DateTime.MinValue;
|
| | | if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return errorValue;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// |
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static bool ObjToBool(this object thisValue)
|
| | | {
|
| | | bool reval = false;
|
| | | if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
|
| | | {
|
| | | return reval;
|
| | | }
|
| | |
|
| | | return reval;
|
| | | }
|
| | |
|
| | |
|
| | | /// <summary>
|
| | | /// 获取当前时间的时间戳
|
| | | /// </summary>
|
| | | /// <param name="thisValue"></param>
|
| | | /// <returns></returns>
|
| | | public static string DateToTimeStamp(this DateTime thisValue)
|
| | | {
|
| | | TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
| | | return Convert.ToInt64(ts.TotalSeconds).ToString();
|
| | | }
|
| | |
|
| | | public static object ChangeType(this object value, Type type)
|
| | | {
|
| | | if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
|
| | | if (value == null) return null;
|
| | | if (type == value.GetType()) return value;
|
| | | if (type.IsEnum)
|
| | | {
|
| | | if (value is string)
|
| | | return Enum.Parse(type, value as string);
|
| | | else
|
| | | return Enum.ToObject(type, value);
|
| | | }
|
| | |
|
| | | if (!type.IsInterface && type.IsGenericType)
|
| | | {
|
| | | Type innerType = type.GetGenericArguments()[0];
|
| | | object innerValue = ChangeType(value, innerType);
|
| | | return Activator.CreateInstance(type, new object[] { innerValue });
|
| | | }
|
| | |
|
| | | if (value is string && type == typeof(Guid)) return new Guid(value as string);
|
| | | if (value is string && type == typeof(Version)) return new Version(value as string);
|
| | | if (!(value is IConvertible)) return value;
|
| | | return Convert.ChangeType(value, type);
|
| | | }
|
| | |
|
| | | public static object ChangeTypeList(this object value, Type type)
|
| | | {
|
| | | if (value == null) return default;
|
| | |
|
| | | var gt = typeof(List<>).MakeGenericType(type);
|
| | | dynamic lis = Activator.CreateInstance(gt);
|
| | |
|
| | | var addMethod = gt.GetMethod("Add");
|
| | | string values = value.ToString();
|
| | | if (values != null && values.StartsWith("(") && values.EndsWith(")"))
|
| | | {
|
| | | string[] splits;
|
| | | if (values.Contains("\",\""))
|
| | | {
|
| | | splits = values.Remove(values.Length - 2, 2)
|
| | | .Remove(0, 2)
|
| | | .Split("\",\"");
|
| | | }
|
| | | else
|
| | | {
|
| | | splits = values.Remove(0, 1)
|
| | | .Remove(values.Length - 2, 1)
|
| | | .Split(",");
|
| | | }
|
| | |
|
| | | foreach (var split in splits)
|
| | | {
|
| | | var str = split;
|
| | | if (split.StartsWith("\"") && split.EndsWith("\""))
|
| | | {
|
| | | str = split.Remove(0, 1)
|
| | | .Remove(split.Length - 2, 1);
|
| | | }
|
| | |
|
| | | addMethod.Invoke(lis, new object[] { ChangeType(str, type) });
|
| | | }
|
| | | }
|
| | |
|
| | | return lis;
|
| | | }
|
| | |
|
| | | public static string ToJson(this object value)
|
| | | {
|
| | | return JsonConvert.SerializeObject(value);
|
| | | }
|
| | |
|
| | | public static bool IsNumeric(this object value)
|
| | | {
|
| | | if (value == null)
|
| | | return false;
|
| | | bool reslut = decimal.TryParse(value.ToString(), out decimal _number);
|
| | | return reslut;
|
| | | }
|
| | |
|
| | | public static bool IsInt(this object obj)
|
| | | {
|
| | | if (obj == null)
|
| | | return false;
|
| | | bool reslut = Int32.TryParse(obj.ToString(), out int _number);
|
| | | return reslut;
|
| | |
|
| | | }
|
| | | public static bool IsDate(this object str)
|
| | | {
|
| | | return str.IsDate(out _);
|
| | | }
|
| | | public static bool IsDate(this object str, out DateTime dateTime)
|
| | | {
|
| | | dateTime = DateTime.Now;
|
| | | if (str == null || str.ToString() == "")
|
| | | {
|
| | | return false;
|
| | | }
|
| | | return DateTime.TryParse(str.ToString(), out dateTime);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 根据传入格式判断是否为小数
|
| | | /// </summary>
|
| | | /// <param name="str"></param>
|
| | | /// <param name="formatString">18,5</param>
|
| | | /// <returns></returns>
|
| | | public static bool IsNumber(this string str, string formatString)
|
| | | {
|
| | | if (string.IsNullOrEmpty(str)) return false;
|
| | |
|
| | | return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$");
|
| | | }
|
| | |
|
| | | public static bool IsGuid(this string guid)
|
| | | {
|
| | | Guid newId;
|
| | | return guid.GetGuid(out newId);
|
| | | }
|
| | |
|
| | | public static bool GetGuid(this string guid, out Guid outId)
|
| | | {
|
| | | Guid emptyId = Guid.Empty;
|
| | | return Guid.TryParse(guid, out outId);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 转换为短整型 qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <param name="data">数据</param>
|
| | | public static short ToShort(this object data)
|
| | | {
|
| | | if (data == null)
|
| | | return 0;
|
| | | short result;
|
| | | var success = short.TryParse(data.ToString(), out result);
|
| | | if (success)
|
| | | return result;
|
| | | try
|
| | | {
|
| | | return Convert.ToInt16(data);
|
| | | }
|
| | | catch (Exception)
|
| | | {
|
| | | return 0;
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 转换为高精度浮点数,并按指定的小数位4舍5入 qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <param name="data">数据</param>
|
| | | /// <param name="digits">小数位数</param>
|
| | | public static decimal ToDecimal(this object data, int digits)
|
| | | {
|
| | | return Math.Round(ObjToDecimal(data), digits);
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | /// <summary>
|
| | | /// 枚举转List qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <typeparam name="T"></typeparam>
|
| | | /// <returns></returns>
|
| | | public static List<object> GetEnumList<T>()
|
| | | {
|
| | | List<object> list = new List<object>();
|
| | | var t = typeof(T);
|
| | | foreach (var item in Enum.GetValues(typeof(T)))
|
| | | {
|
| | | FieldInfo f = t.GetField(Enum.GetName(typeof(T), item));
|
| | | var r = (System.ComponentModel.DescriptionAttribute[])f.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
|
| | | list.Add(new { ID = item, value = Enum.GetName(typeof(T), item), name = r[0].Description });
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | /// <summary> |
| | | /// 验证动态类型元素是否为空 qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <param name="obj"></param>
|
| | | /// <returns></returns>
|
| | | public static bool CheckDynamic(object obj)
|
| | | {
|
| | | return (obj != null && Convert.ToString(obj) != "") ? true : false;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 动态类型元素转字符串并解码 qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <param name="obj"></param>
|
| | | /// <returns></returns>
|
| | | public static string DynamicToString(object obj)
|
| | | {
|
| | | return CheckDynamic(obj) ? HttpUtility.UrlDecode(Convert.ToString(obj).Trim()) : "";
|
| | | }
|
| | |
|
| | | /// <summary> |
| | | /// DataTable转为json qy 2024-7-15
|
| | | /// </summary> |
| | | /// <param name="parObjRefDataTable">DataTable</param> |
| | | /// <returns>json数据</returns> |
| | | public static string ToJson(this DataTable parObjRefDataTable)
|
| | | {
|
| | | List<object> objArrList = new List<object>();
|
| | |
|
| | | foreach (DataRow objDataRow in parObjRefDataTable.Rows)
|
| | | {
|
| | | Dictionary<string, object> objResult = new Dictionary<string, object>();
|
| | |
|
| | | foreach (DataColumn objDataColumn in parObjRefDataTable.Columns)
|
| | | objResult.Add(objDataColumn.ColumnName, objDataRow[objDataColumn].ToString());
|
| | |
|
| | | objArrList.Add(objResult);
|
| | | }
|
| | |
|
| | | return ToJson(objArrList);
|
| | | }
|
| | | /// <summary>
|
| | | /// Json转List qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <typeparam name="T"></typeparam>
|
| | | /// <param name="parJson"></param>
|
| | | /// <returns></returns>
|
| | | public static List<T> JsonToList<T>(this string parJson)
|
| | | {
|
| | | if (string.IsNullOrWhiteSpace(parJson))
|
| | | {
|
| | | return new List<T>();
|
| | | }
|
| | | else
|
| | | {
|
| | | return JsonConvert.DeserializeObject<List<T>>(parJson);
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 日期转为unix值
|
| | | /// </summary>
|
| | | /// <param name="dt"></param>
|
| | | /// <param name="utc">T:按UTC时间计算(默认);F:按本地时间计算</param>
|
| | | /// <returns></returns>
|
| | | /// <summary>
|
| | | /// 日期转为unix值
|
| | | /// </summary>
|
| | | /// <param name="dt">日期时间</param>
|
| | | /// <param name="utc">T:按UTC时间计算(默认);F:按本地时间计算</param>
|
| | | /// <returns></returns>
|
| | | public static double ToUnix(this DateTime dt, bool utc = true)
|
| | | {
|
| | | double intResult = 0;
|
| | | System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
|
| | | intResult = (dt - startTime).TotalSeconds;
|
| | | intResult = Math.Round(intResult, 0);
|
| | | return intResult;
|
| | | }
|
| | |
|
| | | #region 判断是否为字符串 qy 2024-7-15
|
| | | /// <summary>
|
| | | /// 判断字符串是否为合法的json字符串 qy 2024-7-15
|
| | | /// </summary>
|
| | | /// <param name="json"></param>
|
| | | /// <returns></returns>
|
| | | public static bool IsJson(string json)
|
| | | {
|
| | | int errIndex;
|
| | | return IsJson(json, out errIndex);
|
| | | }
|
| | | public static bool IsJson(string json, out int errIndex)
|
| | | {
|
| | | errIndex = 0;
|
| | | if (IsJsonStart(ref json))
|
| | | {
|
| | | CharState cs = new CharState();
|
| | | char c;
|
| | | for (int i = 0; i < json.Length; i++)
|
| | | {
|
| | | c = json[i];
|
| | | if (SetCharState(c, ref cs) && cs.childrenStart)//设置关键符号状态。
|
| | | {
|
| | | string item = json.Substring(i);
|
| | | int err;
|
| | | int length = GetValueLength(item, true, out err);
|
| | | cs.childrenStart = false;
|
| | | if (err > 0)
|
| | | {
|
| | | errIndex = i + err;
|
| | | return false;
|
| | | }
|
| | | i = i + length - 1;
|
| | | }
|
| | | if (cs.isError)
|
| | | {
|
| | | errIndex = i;
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | return !cs.arrayStart && !cs.jsonStart;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | private static bool IsJsonStart(ref string json)
|
| | | {
|
| | | if (!string.IsNullOrEmpty(json))
|
| | | {
|
| | | json = json.Trim('\r', '\n', ' ');
|
| | | if (json.Length > 1)
|
| | | {
|
| | | char s = json[0];
|
| | | char e = json[json.Length - 1];
|
| | | return (s == '{' && e == '}') || (s == '[' && e == ']');
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 获取值的长度(当Json值嵌套以"{"或"["开头时) qy 2024-7-15
|
| | | /// </summary>
|
| | | private static int GetValueLength(string json, bool breakOnErr, out int errIndex)
|
| | | {
|
| | | errIndex = 0;
|
| | | int len = 0;
|
| | | if (!string.IsNullOrEmpty(json))
|
| | | {
|
| | | CharState cs = new CharState();
|
| | | char c;
|
| | | for (int i = 0; i < json.Length; i++)
|
| | | {
|
| | | c = json[i];
|
| | | if (!SetCharState(c, ref cs))//设置关键符号状态。
|
| | | {
|
| | | if (!cs.jsonStart && !cs.arrayStart)//json结束,又不是数组,则退出。
|
| | | {
|
| | | break;
|
| | | }
|
| | | }
|
| | | else if (cs.childrenStart)//正常字符,值状态下。
|
| | | {
|
| | | int length = GetValueLength(json.Substring(i), breakOnErr, out errIndex);//递归子值,返回一个长度。。。
|
| | | cs.childrenStart = false;
|
| | | cs.valueStart = 0;
|
| | | //cs.state = 0;
|
| | | i = i + length - 1;
|
| | | }
|
| | | if (breakOnErr && cs.isError)
|
| | | {
|
| | | errIndex = i;
|
| | | return i;
|
| | | }
|
| | | if (!cs.jsonStart && !cs.arrayStart)//记录当前结束位置。
|
| | | {
|
| | | len = i + 1;//长度比索引+1
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | return len;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 字符状态 qy 2024-7-15
|
| | | /// </summary>
|
| | | private class CharState
|
| | | {
|
| | | internal bool jsonStart = false;//以 "{"开始了...
|
| | | internal bool setDicValue = false;// 可以设置字典值了。
|
| | | internal bool escapeChar = false;//以"\"转义符号开始了
|
| | | /// <summary>
|
| | | /// 数组开始【仅第一开头才算】,值嵌套的以【childrenStart】来标识。
|
| | | /// </summary>
|
| | | internal bool arrayStart = false;//以"[" 符号开始了
|
| | | internal bool childrenStart = false;//子级嵌套开始了。
|
| | | /// <summary>
|
| | | /// 【0 初始状态,或 遇到“,”逗号】;【1 遇到“:”冒号】
|
| | | /// </summary>
|
| | | internal int state = 0;
|
| | |
|
| | | /// <summary>
|
| | | /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】
|
| | | /// </summary>
|
| | | internal int keyStart = 0;
|
| | | /// <summary>
|
| | | /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】
|
| | | /// </summary>
|
| | | internal int valueStart = 0;
|
| | | internal bool isError = false;//是否语法错误。
|
| | |
|
| | | internal void CheckIsError(char c)//只当成一级处理(因为GetLength会递归到每一个子项处理)
|
| | | {
|
| | | if (keyStart > 1 || valueStart > 1)
|
| | | {
|
| | | return;
|
| | | }
|
| | | //示例 ["aa",{"bbbb":123,"fff","ddd"}] |
| | | switch (c)
|
| | | {
|
| | | case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
|
| | | isError = jsonStart && state == 0;//重复开始错误 同时不是值处理。
|
| | | break;
|
| | | case '}':
|
| | | isError = !jsonStart || (keyStart != 0 && state == 0);//重复结束错误 或者 提前结束{"aa"}。正常的有{}
|
| | | break;
|
| | | case '[':
|
| | | isError = arrayStart && state == 0;//重复开始错误
|
| | | break;
|
| | | case ']':
|
| | | isError = !arrayStart || jsonStart;//重复开始错误 或者 Json 未结束
|
| | | break;
|
| | | case '"':
|
| | | case '\'':
|
| | | isError = !(jsonStart || arrayStart); //json 或数组开始。
|
| | | if (!isError)
|
| | | {
|
| | | //重复开始 [""",{"" "}]
|
| | | isError = (state == 0 && keyStart == -1) || (state == 1 && valueStart == -1);
|
| | | }
|
| | | if (!isError && arrayStart && !jsonStart && c == '\'')//['aa',{}]
|
| | | {
|
| | | isError = true;
|
| | | }
|
| | | break;
|
| | | case ':':
|
| | | isError = !jsonStart || state == 1;//重复出现。
|
| | | break;
|
| | | case ',':
|
| | | isError = !(jsonStart || arrayStart); //json 或数组开始。
|
| | | if (!isError)
|
| | | {
|
| | | if (jsonStart)
|
| | | {
|
| | | isError = state == 0 || (state == 1 && valueStart > 1);//重复出现。
|
| | | }
|
| | | else if (arrayStart)//["aa,] [,] [{},{}]
|
| | | {
|
| | | isError = keyStart == 0 && !setDicValue;
|
| | | }
|
| | | }
|
| | | break;
|
| | | case ' ':
|
| | | case '\r':
|
| | | case '\n'://[ "a",\r\n{} ]
|
| | | case '\0':
|
| | | case '\t':
|
| | | break;
|
| | | default: //值开头。。
|
| | | isError = (!jsonStart && !arrayStart) || (state == 0 && keyStart == -1) || (valueStart == -1 && state == 1);//
|
| | | break;
|
| | | }
|
| | | //if (isError)
|
| | | //{
|
| | |
|
| | | //}
|
| | | }
|
| | | }
|
| | | /// <summary>
|
| | | /// 设置字符状态(返回true则为关键词,返回false则当为普通字符处理) qy 2024-7-15
|
| | | /// </summary>
|
| | | private static bool SetCharState(char c, ref CharState cs)
|
| | | {
|
| | | cs.CheckIsError(c);
|
| | | switch (c)
|
| | | {
|
| | | case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}]
|
| | | #region 大括号
|
| | | if (cs.keyStart <= 0 && cs.valueStart <= 0)
|
| | | {
|
| | | cs.keyStart = 0;
|
| | | cs.valueStart = 0;
|
| | | if (cs.jsonStart && cs.state == 1)
|
| | | {
|
| | | cs.childrenStart = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | cs.state = 0;
|
| | | }
|
| | | cs.jsonStart = true;//开始。
|
| | | return true;
|
| | | }
|
| | | #endregion
|
| | | break;
|
| | | case '}':
|
| | | #region 大括号结束
|
| | | if (cs.keyStart <= 0 && cs.valueStart < 2 && cs.jsonStart)
|
| | | {
|
| | | cs.jsonStart = false;//正常结束。
|
| | | cs.state = 0;
|
| | | cs.keyStart = 0;
|
| | | cs.valueStart = 0;
|
| | | cs.setDicValue = true;
|
| | | return true;
|
| | | }
|
| | | // cs.isError = !cs.jsonStart && cs.state == 0;
|
| | | #endregion
|
| | | break;
|
| | | case '[':
|
| | | #region 中括号开始
|
| | | if (!cs.jsonStart)
|
| | | {
|
| | | cs.arrayStart = true;
|
| | | return true;
|
| | | }
|
| | | else if (cs.jsonStart && cs.state == 1)
|
| | | {
|
| | | cs.childrenStart = true;
|
| | | return true;
|
| | | }
|
| | | #endregion
|
| | | break;
|
| | | case ']':
|
| | | #region 中括号结束
|
| | | if (cs.arrayStart && !cs.jsonStart && cs.keyStart <= 2 && cs.valueStart <= 0)//[{},333]//这样结束。
|
| | | {
|
| | | cs.keyStart = 0;
|
| | | cs.valueStart = 0;
|
| | | cs.arrayStart = false;
|
| | | return true;
|
| | | }
|
| | | #endregion
|
| | | break;
|
| | | case '"':
|
| | | case '\'':
|
| | | #region 引号
|
| | | if (cs.jsonStart || cs.arrayStart)
|
| | | {
|
| | | if (cs.state == 0)//key阶段,有可能是数组["aa",{}]
|
| | | {
|
| | | if (cs.keyStart <= 0)
|
| | | {
|
| | | cs.keyStart = (c == '"' ? 3 : 2);
|
| | | return true;
|
| | | }
|
| | | else if ((cs.keyStart == 2 && c == '\'') || (cs.keyStart == 3 && c == '"'))
|
| | | {
|
| | | if (!cs.escapeChar)
|
| | | {
|
| | | cs.keyStart = -1;
|
| | | return true;
|
| | | }
|
| | | else
|
| | | {
|
| | | cs.escapeChar = false;
|
| | | }
|
| | | }
|
| | | }
|
| | | else if (cs.state == 1 && cs.jsonStart)//值阶段必须是Json开始了。
|
| | | {
|
| | | if (cs.valueStart <= 0)
|
| | | {
|
| | | cs.valueStart = (c == '"' ? 3 : 2);
|
| | | return true;
|
| | | }
|
| | | else if ((cs.valueStart == 2 && c == '\'') || (cs.valueStart == 3 && c == '"'))
|
| | | {
|
| | | if (!cs.escapeChar)
|
| | | {
|
| | | cs.valueStart = -1;
|
| | | return true;
|
| | | }
|
| | | else
|
| | | {
|
| | | cs.escapeChar = false;
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | }
|
| | | #endregion
|
| | | break;
|
| | | case ':':
|
| | | #region 冒号
|
| | | if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 0)
|
| | | {
|
| | | if (cs.keyStart == 1)
|
| | | {
|
| | | cs.keyStart = -1;
|
| | | }
|
| | | cs.state = 1;
|
| | | return true;
|
| | | }
|
| | | // cs.isError = !cs.jsonStart || (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1);
|
| | | #endregion
|
| | | break;
|
| | | case ',':
|
| | | #region 逗号 //["aa",{aa:12,}]
|
| | |
|
| | | if (cs.jsonStart)
|
| | | {
|
| | | if (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1)
|
| | | {
|
| | | cs.state = 0;
|
| | | cs.keyStart = 0;
|
| | | cs.valueStart = 0;
|
| | | //if (cs.valueStart == 1)
|
| | | //{
|
| | | // cs.valueStart = 0;
|
| | | //}
|
| | | cs.setDicValue = true;
|
| | | return true;
|
| | | }
|
| | | }
|
| | | else if (cs.arrayStart && cs.keyStart <= 2)
|
| | | {
|
| | | cs.keyStart = 0;
|
| | | //if (cs.keyStart == 1)
|
| | | //{
|
| | | // cs.keyStart = -1;
|
| | | //}
|
| | | return true;
|
| | | }
|
| | | #endregion
|
| | | break;
|
| | | case ' ':
|
| | | case '\r':
|
| | | case '\n'://[ "a",\r\n{} ]
|
| | | case '\0':
|
| | | case '\t':
|
| | | if (cs.keyStart <= 0 && cs.valueStart <= 0) //cs.jsonStart && |
| | | {
|
| | | return true;//跳过空格。
|
| | | }
|
| | | break;
|
| | | default: //值开头。。
|
| | | if (c == '\\') //转义符号
|
| | | {
|
| | | if (cs.escapeChar)
|
| | | {
|
| | | cs.escapeChar = false;
|
| | | }
|
| | | else
|
| | | {
|
| | | cs.escapeChar = true;
|
| | | return true;
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | cs.escapeChar = false;
|
| | | }
|
| | | if (cs.jsonStart || cs.arrayStart) // Json 或数组开始了。
|
| | | {
|
| | | if (cs.keyStart <= 0 && cs.state == 0)
|
| | | {
|
| | | cs.keyStart = 1;//无引号的
|
| | | }
|
| | | else if (cs.valueStart <= 0 && cs.state == 1 && cs.jsonStart)//只有Json开始才有值。
|
| | | {
|
| | | cs.valueStart = 1;//无引号的
|
| | | }
|
| | | }
|
| | | break;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | public static LinqExpressionType GetLinqCondition(this string stringType)
|
| | | {
|
| | | LinqExpressionType linqExpression;
|
| | | switch (stringType)
|
| | | {
|
| | | case HtmlElementType.Contains:
|
| | | linqExpression = LinqExpressionType.In;
|
| | | break;
|
| | | case HtmlElementType.ThanOrEqual:
|
| | | linqExpression = LinqExpressionType.ThanOrEqual;
|
| | | break;
|
| | | case HtmlElementType.LessOrequal:
|
| | | linqExpression = LinqExpressionType.LessThanOrEqual;
|
| | | break;
|
| | | case HtmlElementType.GT:
|
| | | linqExpression = LinqExpressionType.GreaterThan;
|
| | | break;
|
| | | case HtmlElementType.lt:
|
| | | linqExpression = LinqExpressionType.LessThan;
|
| | | break;
|
| | | case HtmlElementType.like:
|
| | | linqExpression = LinqExpressionType.Contains;
|
| | | break;
|
| | | case HtmlElementType.thanorequal:
|
| | | linqExpression = LinqExpressionType.ThanOrEqual;
|
| | | break;
|
| | | case HtmlElementType.lessorequal:
|
| | | linqExpression = LinqExpressionType.LessThanOrEqual;
|
| | | break; |
| | | break;
|
| | | case HtmlElementType.notequal:
|
| | | linqExpression = LinqExpressionType.NotEqual;
|
| | | break; |
| | | default: |
| | | linqExpression = LinqExpressionType.Equal; |
| | | break; |
| | | } |
| | | return linqExpression; |
| | | } |
| | | } |
| | | } |
| | | break;
|
| | | default:
|
| | | linqExpression = LinqExpressionType.Equal;
|
| | | break;
|
| | | }
|
| | | return linqExpression;
|
| | | }
|
| | | }
|
| | | }
|