TypeExtension.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Log;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Security.Cryptography;
  10. namespace WCS.BaseExtensions
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. public static class TypeExtension
  16. {
  17. /// <summary>
  18. /// 将字符串转换为short
  19. /// </summary>
  20. /// <param name="value">需要转换的字符串</param>
  21. /// <returns></returns>
  22. public static short ToShort(this string value)
  23. {
  24. return Convert.ToInt16(value);
  25. }
  26. /// <summary>
  27. /// 将int转换为short
  28. /// </summary>
  29. /// <param name="value">需要转换的字符串</param>
  30. /// <returns></returns>
  31. public static short ToShort(this int value)
  32. {
  33. return Convert.ToInt16(value);
  34. }
  35. /// <summary>
  36. /// 将decimal转换为short
  37. /// </summary>
  38. /// <param name="value">需要转换的字符串</param>
  39. /// <returns></returns>
  40. public static short ToShort(this decimal value)
  41. {
  42. return Convert.ToInt16(value);
  43. }
  44. /// <summary>
  45. /// 将字符串转换为int
  46. /// </summary>
  47. /// <param name="value">需要转换的字符串</param>
  48. /// <returns></returns>
  49. public static int ToInt(this string value)
  50. {
  51. return Convert.ToInt32(value);
  52. }
  53. /// <summary>
  54. /// 判断值为奇数/偶数
  55. /// </summary>
  56. /// <param name="value">需要判断的值</param>
  57. /// <returns> true:是奇数 false:是偶数</returns>
  58. public static bool OddNumberOrEven(this short value)
  59. {
  60. return value % 2 != 0;
  61. }
  62. /// <summary>
  63. /// 获取short类型Code,只限设备组
  64. /// </summary>
  65. /// <param name="value"></param>
  66. /// <returns></returns>
  67. public static short GetShortCode(this string value)
  68. {
  69. return value.Replace("G", "").ToShort();
  70. }
  71. /// <summary>
  72. /// 数据映射
  73. /// </summary>
  74. /// <typeparam name="D"></typeparam>
  75. /// <typeparam name="S"></typeparam>
  76. /// <param name="s"></param>
  77. /// <returns></returns>
  78. public static D Mapper<D, S>(S s)
  79. {
  80. D d = Activator.CreateInstance<D>();
  81. var sType = s.GetType();
  82. var dType = typeof(D);
  83. foreach (PropertyInfo sP in sType.GetProperties())
  84. {
  85. foreach (PropertyInfo dP in dType.GetProperties())
  86. {
  87. if (dP.Name == sP.Name)
  88. {
  89. dP.SetValue(d, sP.GetValue(s));
  90. break;
  91. }
  92. }
  93. }
  94. return d;
  95. }
  96. /// <summary>
  97. /// 获取字典
  98. /// </summary>
  99. /// <typeparam name="T1"></typeparam>
  100. /// <typeparam name="T2"></typeparam>
  101. /// <typeparam name="T3"></typeparam>
  102. /// <param name="t3"></param>
  103. /// <returns></returns>
  104. public static Dictionary<string, object> EntityClassToDictionary<T>(T t)
  105. {
  106. Type type = typeof(SugarColumn);
  107. Dictionary<string, object> d = new Dictionary<string, object>();
  108. var sType = t.GetType();
  109. foreach (PropertyInfo sP in sType.GetProperties())
  110. {
  111. if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID")
  112. {
  113. d.Add(sP.Name, sP.GetValue(t));
  114. }
  115. }
  116. return d;
  117. }
  118. /// <summary>
  119. /// 获取MD5字符串
  120. /// </summary>
  121. /// <param name="myString"></param>
  122. /// <returns></returns>
  123. public static string GetMD5(this string myString)
  124. {
  125. MD5 md5 = MD5.Create();
  126. byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
  127. byte[] targetData = md5.ComputeHash(fromData);
  128. string byte2String = null;
  129. for (int i = 0; i < targetData.Length; i++)
  130. {
  131. byte2String += targetData[i].ToString("x");
  132. }
  133. return byte2String;
  134. }
  135. /// <summary>
  136. /// DataTable转换成实体类
  137. /// </summary>
  138. /// <typeparam name="T"></typeparam>
  139. /// <param name="dt"></param>
  140. /// <returns></returns>
  141. public static List<object> TableToEntity(this DataTable dt, string typeName)
  142. {
  143. List<object> list = new List<object>();
  144. try
  145. {
  146. foreach (DataRow row in dt.Rows)
  147. {
  148. Type entity = Type.GetType(typeName);
  149. PropertyInfo[] pArray = entity.GetType().GetProperties();
  150. foreach (PropertyInfo p in pArray)
  151. {
  152. if (dt.Columns.Contains(p.Name))
  153. {
  154. if (!p.CanWrite) continue;
  155. var value = row[p.Name];
  156. if (value != DBNull.Value)
  157. {
  158. Type targetType = p.PropertyType;
  159. Type convertType = targetType;
  160. if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  161. {
  162. //可空类型
  163. NullableConverter nullableConverter = new NullableConverter(targetType);
  164. convertType = nullableConverter.UnderlyingType;
  165. }
  166. if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString()))
  167. {
  168. value = Convert.ChangeType(value, convertType);
  169. }
  170. switch (convertType.FullName)
  171. {
  172. case "System.Decimal":
  173. p.SetValue(entity, Convert.ToDecimal(value), null);
  174. break;
  175. case "System.String":
  176. p.SetValue(entity, Convert.ToString(value), null);
  177. break;
  178. case "System.Int32":
  179. p.SetValue(entity, Convert.ToInt32(value), null);
  180. break;
  181. case "System.Int64":
  182. p.SetValue(entity, Convert.ToInt64(value), null);
  183. break;
  184. case "System.Int16":
  185. p.SetValue(entity, Convert.ToInt16(value), null);
  186. break;
  187. case "System.Double":
  188. p.SetValue(entity, Convert.ToDouble(value), null);
  189. break;
  190. case "System.Single":
  191. p.SetValue(entity, Convert.ToSingle(value), null);
  192. break;
  193. case "System.DateTime":
  194. p.SetValue(entity, Convert.ToDateTime(value), null);
  195. break;
  196. default:
  197. p.SetValue(entity, value, null);
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. list.Add(entity);
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. InfoLog.INFO_ERROR("Table转换实体类失败:" + ex.Message);
  209. }
  210. return list;
  211. }
  212. }
  213. }