using Log; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Reflection; using System.Security.Cryptography; namespace WCS.BaseExtensions { /// /// /// public static class TypeExtension { /// /// 将字符串转换为short /// /// 需要转换的字符串 /// public static short ToShort(this string value) { return Convert.ToInt16(value); } /// /// 将int转换为short /// /// 需要转换的字符串 /// public static short ToShort(this int value) { return Convert.ToInt16(value); } /// /// 将decimal转换为short /// /// 需要转换的字符串 /// public static short ToShort(this decimal value) { return Convert.ToInt16(value); } /// /// 将字符串转换为int /// /// 需要转换的字符串 /// public static int ToInt(this string value) { return Convert.ToInt32(value); } /// /// 判断值为奇数/偶数 /// /// 需要判断的值 /// true:是奇数 false:是偶数 public static bool OddNumberOrEven(this short value) { return value % 2 != 0; } /// /// 获取short类型Code,只限设备组 /// /// /// public static short GetShortCode(this string value) { return value.Replace("G", "").ToShort(); } /// /// 数据映射 /// /// /// /// /// public static D Mapper(S s) { D d = Activator.CreateInstance(); var sType = s.GetType(); var dType = typeof(D); foreach (PropertyInfo sP in sType.GetProperties()) { foreach (PropertyInfo dP in dType.GetProperties()) { if (dP.Name == sP.Name) { dP.SetValue(d, sP.GetValue(s)); break; } } } return d; } /// /// 获取字典 /// /// /// /// /// /// public static Dictionary EntityClassToDictionary(T t) { Type type = typeof(SugarColumn); Dictionary d = new Dictionary(); var sType = t.GetType(); foreach (PropertyInfo sP in sType.GetProperties()) { if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID") { d.Add(sP.Name, sP.GetValue(t)); } } return d; } /// /// 获取MD5字符串 /// /// /// public static string GetMD5(this string myString) { MD5 md5 = MD5.Create(); byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString); byte[] targetData = md5.ComputeHash(fromData); string byte2String = null; for (int i = 0; i < targetData.Length; i++) { byte2String += targetData[i].ToString("x"); } return byte2String; } /// /// DataTable转换成实体类 /// /// /// /// public static List TableToEntity(this DataTable dt, string typeName) { List list = new List(); try { foreach (DataRow row in dt.Rows) { Type entity = Type.GetType(typeName); PropertyInfo[] pArray = entity.GetType().GetProperties(); foreach (PropertyInfo p in pArray) { if (dt.Columns.Contains(p.Name)) { if (!p.CanWrite) continue; var value = row[p.Name]; if (value != DBNull.Value) { Type targetType = p.PropertyType; Type convertType = targetType; if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { //可空类型 NullableConverter nullableConverter = new NullableConverter(targetType); convertType = nullableConverter.UnderlyingType; } if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString())) { value = Convert.ChangeType(value, convertType); } switch (convertType.FullName) { case "System.Decimal": p.SetValue(entity, Convert.ToDecimal(value), null); break; case "System.String": p.SetValue(entity, Convert.ToString(value), null); break; case "System.Int32": p.SetValue(entity, Convert.ToInt32(value), null); break; case "System.Int64": p.SetValue(entity, Convert.ToInt64(value), null); break; case "System.Int16": p.SetValue(entity, Convert.ToInt16(value), null); break; case "System.Double": p.SetValue(entity, Convert.ToDouble(value), null); break; case "System.Single": p.SetValue(entity, Convert.ToSingle(value), null); break; case "System.DateTime": p.SetValue(entity, Convert.ToDateTime(value), null); break; default: p.SetValue(entity, value, null); break; } } } } list.Add(entity); } } catch (Exception ex) { InfoLog.INFO_ERROR("Table转换实体类失败:" + ex.Message); } return list; } } }