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

ModelConvertHelper.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace System.Common.Helpers
  7. {
  8. /// <summary>
  9. /// 实体转换辅助类
  10. /// </summary>
  11. public class ModelConvertHelper<T> where T : new()
  12. {
  13. public static IList<T> ConvertToModel(DataTable dt)
  14. {
  15. // 定义集合
  16. IList<T> ts = new List<T>();
  17. // 获得此模型的类型
  18. Type type = typeof(T);
  19. string tempName = "";
  20. foreach (DataRow dr in dt.Rows)
  21. {
  22. int i = 0;
  23. T t = new T();
  24. // 获得此模型的公共属性
  25. PropertyInfo[] propertys = t.GetType().GetProperties();
  26. foreach (PropertyInfo pi in propertys)
  27. {
  28. if (i >= dr.ItemArray.Length)
  29. break;
  30. tempName = pi.Name; // 检查DataTable是否包含此列
  31. //if (dt.Columns.Contains(tempName))
  32. //{
  33. // 判断此属性是否有Setter
  34. if (!pi.CanWrite) continue;
  35. object value = dr[i];//object value = dr[tempName];
  36. if (pi.PropertyType.FullName.Contains(typeof(System.Int64).FullName))
  37. { pi.SetValue(t, Convert.ToInt64(value), null); }
  38. else if (pi.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
  39. { pi.SetValue(t, Convert.ToInt32(value), null); }
  40. else if (pi.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
  41. { pi.SetValue(t, Convert.ToDecimal(value), null); }
  42. else if (pi.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
  43. {
  44. if (value != DBNull.Value && value.ToString() != "")
  45. pi.SetValue(t, Convert.ToDateTime(value), null);
  46. else
  47. pi.SetValue(t, null, null);
  48. }
  49. else
  50. if (value != DBNull.Value)
  51. pi.SetValue(t, value, null);
  52. i++;
  53. //}
  54. }
  55. ts.Add(t);
  56. }
  57. return ts;
  58. }
  59. // **//// <summary>
  60. /// 转换IList<T>为List<T> //将IList接口泛型转为List泛型类型
  61. /// </summary>
  62. /// <typeparam name="T">指定的集合中泛型的类型</typeparam>
  63. /// <param name="gbList">需要转换的IList</param>
  64. /// <returns></returns>
  65. public static List<T> ConvertIListToList<T>(IList<T> gbList) where T : class //静态方法,泛型转换,
  66. {
  67. if (gbList != null && gbList.Count >= 1)
  68. {
  69. List<T> list = new List<T>();
  70. for (int i = 0; i < gbList.Count; i++) //将IList中的元素复制到List中
  71. {
  72. T temp = gbList[i] as T;
  73. if (temp != null)
  74. list.Add(temp);
  75. }
  76. return list;
  77. }
  78. return null;
  79. }
  80. /// <summary>
  81. /// 把DataTable转换成指定类型的List
  82. /// </summary>
  83. /// <param name="dt"></param>
  84. /// <returns></returns>
  85. public static IList<T> ConvertDataTableToList(DataTable dt)
  86. {
  87. // 定义集合
  88. IList<T> ts = new List<T>();
  89. string tempName = "";
  90. foreach (DataRow dr in dt.Rows)
  91. {
  92. T t = new T();
  93. // 获得此模型的公共属性
  94. PropertyInfo[] propertys = t.GetType().GetProperties();
  95. foreach (PropertyInfo pi in propertys)
  96. {
  97. tempName = pi.Name; // 检查DataTable是否包含此列
  98. if (dt.Columns.Contains(tempName))
  99. {
  100. // 判断此属性是否有Setter
  101. if (!pi.CanWrite) continue;
  102. object value = dr[tempName];
  103. if (value != DBNull.Value)
  104. pi.SetValue(t, value, null);
  105. }
  106. }
  107. ts.Add(t);
  108. }
  109. return ts;
  110. }
  111. /// <summary>
  112. /// 把泛型List转换成DataTable
  113. /// </summary>
  114. /// <param name="list"></param>
  115. /// <returns></returns>
  116. public static DataTable ConvertListToDataTable(List<T> list)
  117. {
  118. DataTable dt = new DataTable();
  119. // 获得此模型的公共属性
  120. PropertyInfo[] propertys = typeof(T).GetProperties();
  121. foreach (PropertyInfo pi in propertys)
  122. {
  123. // 判断此属性是否有Getter
  124. if (!pi.CanRead) continue;
  125. dt.Columns.Add(pi.Name, pi.PropertyType);
  126. }
  127. foreach (T item in list)
  128. {
  129. propertys = item.GetType().GetProperties();
  130. DataRow newRow = dt.NewRow();
  131. foreach (PropertyInfo pi in propertys)
  132. {
  133. if (!pi.CanRead) continue;
  134. newRow[pi.Name] = pi.GetValue(item);
  135. }
  136. dt.Rows.Add(newRow);
  137. }
  138. return dt;
  139. }
  140. }
  141. public class ModelConvertHelper<T, T1> where T : new() where T1 : new()
  142. {
  143. /// <summary>
  144. /// 实体相同属性快速赋值转换
  145. /// </summary>
  146. /// <param name="t1"></param>
  147. /// <param name="t2"></param>
  148. /// <returns>返回第一个参数</returns>
  149. public static T ModeToModel(T t1, T1 t2)
  150. {
  151. PropertyInfo[] t1pList = typeof(T).GetProperties();
  152. PropertyInfo[] t2pList = typeof(T1).GetProperties();
  153. foreach (PropertyInfo t2p in t2pList)
  154. {
  155. object t2value = t2p.GetValue(t2);
  156. foreach (var t1p in t1pList)
  157. {
  158. if (t1p.Name == t2p.Name)
  159. {
  160. t1p.SetValue(t1, t2value, null);
  161. }
  162. }
  163. }
  164. return t1;
  165. }
  166. }
  167. }