足力健后端,使用.netcore版本,合并1个项目使用

ModelConvertHelper.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace System.Common.Helpers
  8. {
  9. /// <summary>
  10. /// 实体转换辅助类
  11. /// </summary>
  12. public class ModelConvertHelper<T> where T : new()
  13. {
  14. public static IList<T> ConvertToModel(DataTable dt)
  15. {
  16. // 定义集合
  17. IList<T> ts = new List<T>();
  18. // 获得此模型的类型
  19. Type type = typeof(T);
  20. string tempName = "";
  21. foreach (DataRow dr in dt.Rows)
  22. {
  23. int i = 0;
  24. T t = new T();
  25. // 获得此模型的公共属性
  26. PropertyInfo[] propertys = t.GetType().GetProperties();
  27. foreach (PropertyInfo pi in propertys)
  28. {
  29. if (i >= dr.ItemArray.Length)
  30. break;
  31. tempName = pi.Name; // 检查DataTable是否包含此列
  32. //if (dt.Columns.Contains(tempName))
  33. //{
  34. // 判断此属性是否有Setter
  35. if (!pi.CanWrite) continue;
  36. object value = dr[i];//object value = dr[tempName];
  37. if (pi.PropertyType.FullName.Contains(typeof(System.Int64).FullName))
  38. { pi.SetValue(t, Convert.ToInt64(value), null); }
  39. else if (pi.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  40. { pi.SetValue(t, Convert.ToInt32(value), null); }
  41. else if (pi.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  42. { pi.SetValue(t, Convert.ToDecimal(value), null); }
  43. else if (pi.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  44. {
  45. if (value != DBNull.Value && value.ToString() != "")
  46. pi.SetValue(t, Convert.ToDateTime(value), null);
  47. else
  48. pi.SetValue(t, null, null);
  49. }
  50. else
  51. if (value != DBNull.Value)
  52. pi.SetValue(t, value, null);
  53. i++;
  54. //}
  55. }
  56. ts.Add(t);
  57. }
  58. return ts;
  59. }
  60. /// <summary>
  61. /// 带列名的DataTable 转实体对象
  62. /// </summary>
  63. /// <param name="dt">带列名的DataTable</param>
  64. /// <param name="dirList">第一个参数是实体列名【小写格式】,第二个参数是相对应table的列名</param>
  65. /// <returns></returns>
  66. public static IList<T> ConvertToModelByColumns(DataTable dt, Dictionary<string, string> dirList)
  67. {
  68. // 定义集合
  69. IList<T> ts = new List<T>();
  70. // 获得此模型的类型
  71. Type type = typeof(T);
  72. string tempName = "";
  73. foreach (DataRow dr in dt.Rows)
  74. {
  75. int i = 0;
  76. T t = new T();
  77. // 获得此模型的公共属性
  78. PropertyInfo[] propertys = t.GetType().GetProperties();
  79. foreach (PropertyInfo pi in propertys)
  80. {
  81. if (i >= dr.ItemArray.Length)
  82. break;
  83. tempName = pi.Name.ToLower(); // 检查DataTable是否包含此列
  84. if (dirList.ContainsKey(tempName))
  85. {
  86. // 判断此属性是否有Setter
  87. if (!pi.CanWrite) continue;
  88. object value = dr[dirList[tempName]];
  89. if (pi.PropertyType.FullName.Contains(typeof(System.Int64).FullName))
  90. { pi.SetValue(t, Convert.ToInt64(value), null); }
  91. else if (pi.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  92. { pi.SetValue(t, Convert.ToInt32(value), null); }
  93. else if (pi.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  94. { pi.SetValue(t, Convert.ToDecimal(value), null); }
  95. else if (pi.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  96. {
  97. if (value != DBNull.Value && value.ToString() != "")
  98. pi.SetValue(t, Convert.ToDateTime(value), null);
  99. else
  100. pi.SetValue(t, null, null);
  101. }
  102. else
  103. if (value != DBNull.Value)
  104. pi.SetValue(t, value, null);
  105. i++;
  106. }
  107. }
  108. ts.Add(t);
  109. }
  110. return ts;
  111. }
  112. // **//// <summary>
  113. /// 转换IList<T>为List<T> //将IList接口泛型转为List泛型类型
  114. /// </summary>
  115. /// <typeparam name="T">指定的集合中泛型的类型</typeparam>
  116. /// <param name="gbList">需要转换的IList</param>
  117. /// <returns></returns>
  118. public static List<T> ConvertIListToList<T>(IList<T> gbList) where T : class //静态方法,泛型转换,
  119. {
  120. if (gbList != null && gbList.Count >= 1)
  121. {
  122. List<T> list = new List<T>();
  123. for (int i = 0; i < gbList.Count; i++) //将IList中的元素复制到List中
  124. {
  125. T temp = gbList[i] as T;
  126. if (temp != null)
  127. list.Add(temp);
  128. }
  129. return list;
  130. }
  131. return null;
  132. }
  133. /// <summary>
  134. /// 把DataTable转换成指定类型的List
  135. /// </summary>
  136. /// <param name="dt"></param>
  137. /// <returns></returns>
  138. public static IList<T> ConvertDataTableToList(DataTable dt)
  139. {
  140. // 定义集合
  141. IList<T> ts = new List<T>();
  142. string tempName = "";
  143. foreach (DataRow dr in dt.Rows)
  144. {
  145. T t = new T();
  146. // 获得此模型的公共属性
  147. PropertyInfo[] propertys = t.GetType().GetProperties();
  148. foreach (PropertyInfo pi in propertys)
  149. {
  150. tempName = pi.Name; // 检查DataTable是否包含此列
  151. if (dt.Columns.Contains(tempName))
  152. {
  153. // 判断此属性是否有Setter
  154. if (!pi.CanWrite) continue;
  155. object value = dr[tempName];
  156. if (value != DBNull.Value)
  157. pi.SetValue(t, value, null);
  158. }
  159. }
  160. ts.Add(t);
  161. }
  162. return ts;
  163. }
  164. /// <summary>
  165. /// 把泛型List转换成DataTable
  166. /// </summary>
  167. /// <param name="list"></param>
  168. /// <returns></returns>
  169. public static DataTable ConvertListToDataTable(List<T> list)
  170. {
  171. DataTable dt = new DataTable();
  172. // 获得此模型的公共属性
  173. PropertyInfo[] propertys = typeof(T).GetProperties();
  174. foreach (PropertyInfo pi in propertys)
  175. {
  176. // 判断此属性是否有Getter
  177. if (!pi.CanRead) continue;
  178. dt.Columns.Add(pi.Name, pi.PropertyType);
  179. }
  180. foreach (T item in list)
  181. {
  182. propertys = item.GetType().GetProperties();
  183. DataRow newRow = dt.NewRow();
  184. foreach (PropertyInfo pi in propertys)
  185. {
  186. if (!pi.CanRead) continue;
  187. newRow[pi.Name] = pi.GetValue(item);
  188. }
  189. dt.Rows.Add(newRow);
  190. }
  191. return dt;
  192. }
  193. }
  194. public class ModelConvertHelper<T, T1> where T : new() where T1 : new()
  195. {
  196. /// <summary>
  197. /// 实体相同属性快速赋值转换 t2赋值给t1
  198. /// </summary>
  199. /// <param name="t1"></param>
  200. /// <param name="t2"></param>
  201. /// <returns>返回第一个参数</returns>
  202. public static T ModeToModel(T t1, T1 t2)
  203. {
  204. PropertyInfo[] t1pList = typeof(T).GetProperties();
  205. PropertyInfo[] t2pList = typeof(T1).GetProperties();
  206. foreach (PropertyInfo t2p in t2pList)
  207. {
  208. object t2value = t2p.GetValue(t2);
  209. foreach (var t1p in t1pList)
  210. {
  211. if (t1p.Name == t2p.Name)
  212. {
  213. t1p.SetValue(t1, t2value, null);
  214. break;
  215. }
  216. }
  217. }
  218. return t1;
  219. }
  220. /// <summary>
  221. /// 实体相同属性快速赋值转换 t2赋值给t1 不符合赋值规则的则不进行赋值
  222. /// </summary>
  223. /// <param name="t1"></param>
  224. /// <param name="t2"></param>
  225. /// <returns>返回第一个参数</returns>
  226. public static T ModeToModelDefault(T t1, T1 t2)
  227. {
  228. PropertyInfo[] t1pList = typeof(T).GetProperties();
  229. PropertyInfo[] t2pList = typeof(T1).GetProperties();
  230. foreach (PropertyInfo t2p in t2pList)
  231. {
  232. object t2value = t2p.GetValue(t2);
  233. foreach (var t1p in t1pList)
  234. {
  235. if (t1p.Name.ToLower() == t2p.Name.ToLower() || t1p.Name.ToUpper() == "F_" + t2p.Name.ToUpper())
  236. {
  237. if (t2value == DBNull.Value || t2value == null)
  238. {
  239. }
  240. else if (t1p.PropertyType.FullName.Contains(typeof(System.Int64).FullName) || t1p.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  241. {
  242. if (t2value.ObjToInt() > 0)
  243. t1p.SetValue(t1, t2value, null);
  244. }
  245. else if (t1p.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  246. {
  247. t1p.SetValue(t1, t2value, null);
  248. }
  249. else if (t1p.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  250. {
  251. if (t2value != DBNull.Value && t2value.ToString() != "")
  252. t1p.SetValue(t1, Convert.ToDateTime(t2value), null);
  253. else
  254. t1p.SetValue(t1, null, null);
  255. }
  256. else
  257. {
  258. if (t2value.ToString() != "")
  259. t1p.SetValue(t1, t2value, null);
  260. }
  261. break;
  262. }
  263. }
  264. }
  265. return t1;
  266. }
  267. }
  268. }