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

ModelConvertHelper.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. if (pi.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  179. {
  180. dt.Columns.Add(pi.Name, typeof(System.DateTime));
  181. }
  182. else if (pi.PropertyType.FullName.Contains(typeof(System.Int64).FullName))
  183. {
  184. dt.Columns.Add(pi.Name, typeof(System.Int64));
  185. }
  186. else if (pi.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  187. {
  188. dt.Columns.Add(pi.Name, typeof(System.Int32));
  189. }
  190. else if (pi.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  191. {
  192. dt.Columns.Add(pi.Name, typeof(System.Decimal));
  193. }
  194. else { dt.Columns.Add(pi.Name, pi.PropertyType); }
  195. }
  196. foreach (T item in list)
  197. {
  198. propertys = item.GetType().GetProperties();
  199. DataRow newRow = dt.NewRow();
  200. foreach (PropertyInfo pi in propertys)
  201. {
  202. if (!pi.CanRead) continue;
  203. if (pi.GetValue(item) == DBNull.Value || pi.GetValue(item) == null)
  204. {
  205. newRow[pi.Name] = DBNull.Value;
  206. }
  207. else { newRow[pi.Name] = pi.GetValue(item); }
  208. }
  209. dt.Rows.Add(newRow);
  210. }
  211. return dt;
  212. }
  213. }
  214. public class ModelConvertHelper<T, T1> where T : new() where T1 : new()
  215. {
  216. /// <summary>
  217. /// 实体相同属性快速赋值转换 t2赋值给t1
  218. /// </summary>
  219. /// <param name="t1"></param>
  220. /// <param name="t2"></param>
  221. /// <returns>返回第一个参数</returns>
  222. public static T ModeToModel(T t1, T1 t2)
  223. {
  224. PropertyInfo[] t1pList = typeof(T).GetProperties();
  225. PropertyInfo[] t2pList = typeof(T1).GetProperties();
  226. foreach (PropertyInfo t2p in t2pList)
  227. {
  228. object t2value = t2p.GetValue(t2);
  229. foreach (var t1p in t1pList)
  230. {
  231. if (t1p.Name == t2p.Name)
  232. {
  233. t1p.SetValue(t1, t2value, null);
  234. break;
  235. }
  236. }
  237. }
  238. return t1;
  239. }
  240. /// <summary>
  241. /// 实体相同属性快速赋值转换 t2赋值给t1 不符合赋值规则的则不进行赋值
  242. /// </summary>
  243. /// <param name="t1"></param>
  244. /// <param name="t2"></param>
  245. /// <returns>返回第一个参数</returns>
  246. public static T ModeToModelDefault(T t1, T1 t2)
  247. {
  248. PropertyInfo[] t1pList = typeof(T).GetProperties();
  249. PropertyInfo[] t2pList = typeof(T1).GetProperties();
  250. foreach (PropertyInfo t2p in t2pList)
  251. {
  252. object t2value = t2p.GetValue(t2);
  253. foreach (var t1p in t1pList)
  254. {
  255. if (t1p.Name.ToLower() == t2p.Name.ToLower() || t1p.Name.ToUpper() == "F_" + t2p.Name.ToUpper())
  256. {
  257. if (t2value == DBNull.Value || t2value == null)
  258. {
  259. }
  260. else if (t1p.PropertyType.FullName.Contains(typeof(System.Int64).FullName) || t1p.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  261. {
  262. if (t2value.ObjToInt() > 0)
  263. t1p.SetValue(t1, t2value, null);
  264. }
  265. else if (t1p.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  266. {
  267. t1p.SetValue(t1, t2value, null);
  268. }
  269. else if (t1p.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  270. {
  271. if (t2value != DBNull.Value && t2value.ToString() != "")
  272. t1p.SetValue(t1, Convert.ToDateTime(t2value), null);
  273. else
  274. t1p.SetValue(t1, null, null);
  275. }
  276. else
  277. {
  278. if (t2value.ToString() != "")
  279. t1p.SetValue(t1, t2value, null);
  280. }
  281. break;
  282. }
  283. }
  284. }
  285. return t1;
  286. }
  287. }
  288. }