FuncTable2Entity.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.ComponentModel;
  7. namespace WMS.Util
  8. {
  9. /// <summary>
  10. /// Table转换实体处理
  11. /// </summary>
  12. public class FuncTable2Entity
  13. {
  14. /// <summary>
  15. /// DataTable转换成对象的List集合
  16. /// </summary>
  17. /// <typeparam name="T">对象类型</typeparam>
  18. /// <param name="_tab">DataTable</param>
  19. /// <returns></returns>
  20. public static List<T> DataTableToList<T>(DataTable _tab,out string errorMsg) where T : new()
  21. {
  22. errorMsg = string.Empty;
  23. List<T> _list = new List<T>();
  24. if (_tab == null) return _list;
  25. System.Reflection.PropertyInfo[] _infos = typeof(T).GetProperties();
  26. foreach (DataRow _row in _tab.Rows)
  27. {
  28. T _t = new T();
  29. foreach (System.Reflection.PropertyInfo property in _infos)
  30. if (property.CanWrite && _tab.Columns.Contains(property.Name))
  31. {
  32. DataRowToEntity<T>(property, _row, _t, out string msg);
  33. if (!string.IsNullOrWhiteSpace(msg))
  34. {
  35. errorMsg =$"第{_tab.Rows.IndexOf(_row)+1} 行发生错误:{msg}";
  36. return null;
  37. }
  38. }
  39. _list.Add(_t);
  40. }
  41. return _list;
  42. }
  43. public static List<T> DataTableToList<T>(DataTable _tab) where T : new()
  44. {
  45. List<T> _list = new List<T>();
  46. if (_tab == null) return _list;
  47. System.Reflection.PropertyInfo[] _infos = typeof(T).GetProperties();
  48. foreach (DataRow _row in _tab.Rows)
  49. {
  50. T _t = new T();
  51. foreach (System.Reflection.PropertyInfo property in _infos)
  52. if (property.CanWrite && _tab.Columns.Contains(property.Name))
  53. DataRowToEntity<T>(property, _row, _t, out string msg);
  54. _list.Add(_t);
  55. }
  56. return _list;
  57. }
  58. /// <summary>
  59. /// DataTable转换成对象的List集合
  60. /// </summary>
  61. /// <typeparam name="Parent"></typeparam>
  62. /// <typeparam name="Child"></typeparam>
  63. /// <param name="_tab"></param>
  64. /// <returns></returns>
  65. public static List<Parent> DataTableToList<Parent, Child>(DataTable _tab)
  66. where Parent : new()
  67. where Child : new()
  68. {
  69. List<Parent> _list = new List<Parent>();
  70. if (_tab == null) return _list;
  71. foreach (DataRow _row in _tab.Rows)
  72. _list.Add(DataRowToEntity<Parent, Child>(_row));
  73. return _list;
  74. }
  75. /// <summary>
  76. /// DataTable转换成对象的List集合
  77. /// </summary>
  78. /// <typeparam name="Parent"></typeparam>
  79. /// <typeparam name="ChildParent"></typeparam>
  80. /// <typeparam name="Child"></typeparam>
  81. /// <param name="_tab"></param>
  82. /// <returns></returns>
  83. public static List<Parent> DataTableToList<Parent, ChildParent, Child>(DataTable _tab)
  84. where Parent : new()
  85. where ChildParent : new()
  86. where Child : new()
  87. {
  88. List<Parent> _list = new List<Parent>();
  89. if (_tab == null) return _list;
  90. System.Reflection.PropertyInfo[] _infos = typeof(Parent).GetProperties();
  91. foreach (DataRow _row in _tab.Rows)
  92. {
  93. Parent _t = new Parent();
  94. foreach (System.Reflection.PropertyInfo property in _infos)
  95. if (property.CanWrite && property.PropertyType.Name == typeof(ChildParent).Name)
  96. property.SetValue(_t, DataRowToEntity<ChildParent, Child>(_row), null);
  97. else if (property.CanWrite && _tab.Columns.Contains(property.Name))
  98. DataRowToEntity<Parent>(property, _row, _t, out string msg);
  99. _list.Add(_t);
  100. }
  101. return _list;
  102. }
  103. /// <summary>
  104. /// DataTable转换成对象的List集合
  105. /// </summary>
  106. /// <typeparam name="Parent"></typeparam>
  107. /// <typeparam name="Child1"></typeparam>
  108. /// <typeparam name="Child2"></typeparam>
  109. /// <param name="_tab"></param>
  110. /// <returns></returns>
  111. public static List<Parent> DataTableToList2Child<Parent, Child1, Child2>(DataTable _tab)
  112. where Parent : new()
  113. where Child1 : new()
  114. where Child2 : new()
  115. {
  116. List<Parent> _list = new List<Parent>();
  117. if (_tab == null) return _list;
  118. System.Reflection.PropertyInfo[] _infos = typeof(Parent).GetProperties();
  119. foreach (DataRow _row in _tab.Rows)
  120. {
  121. Parent _t = new Parent();
  122. foreach (System.Reflection.PropertyInfo property in _infos)
  123. if (property.CanWrite && property.PropertyType.Name == typeof(Child1).Name)
  124. property.SetValue(_t, DataRowToEntity<Child1>(_row), null);
  125. else if (property.CanWrite && property.PropertyType.Name == typeof(Child2).Name)
  126. property.SetValue(_t, DataRowToEntity<Child2>(_row), null);
  127. else if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
  128. DataRowToEntity<Parent>(property, _row, _t, out string msg);
  129. _list.Add(_t);
  130. }
  131. return _list;
  132. }
  133. /// <summary>
  134. /// 把DataRow转换成实体
  135. /// </summary>
  136. /// <typeparam name="T"></typeparam>
  137. /// <param name="_row"></param>
  138. /// <returns></returns>
  139. public static T DataRowToEntity<T>(DataRow _row) where T : new()
  140. {
  141. T _t = new T();
  142. foreach (System.Reflection.PropertyInfo property in typeof(T).GetProperties())
  143. if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
  144. DataRowToEntity<T>(property, _row, _t, out string msg);
  145. return _t;
  146. }
  147. /// <summary>
  148. /// 把DataRow转换成实体
  149. /// </summary>
  150. /// <typeparam name="Parent"></typeparam>
  151. /// <typeparam name="Child"></typeparam>
  152. /// <param name="_row"></param>
  153. /// <returns></returns>
  154. public static Parent DataRowToEntity<Parent, Child>(DataRow _row)
  155. where Parent : new()
  156. where Child : new()
  157. {
  158. Parent _t = new Parent();
  159. foreach (System.Reflection.PropertyInfo property in typeof(Parent).GetProperties())
  160. if (property.CanWrite && property.PropertyType.Name == typeof(Child).Name)
  161. property.SetValue(_t, DataRowToEntity<Child>(_row), null);
  162. else if (property.CanWrite && _row.Table.Columns.Contains(property.Name))
  163. DataRowToEntity<Parent>(property, _row, _t ,out string msg);
  164. return _t;
  165. }
  166. /// <summary>
  167. /// 把DataRow转换成实体
  168. /// </summary>
  169. /// <typeparam name="T"></typeparam>
  170. /// <param name="property"></param>
  171. /// <param name="_row"></param>
  172. /// <param name="_t"></param>
  173. /// <returns></returns>
  174. public static T DataRowToEntity<T>(System.Reflection.PropertyInfo property, DataRow _row, T _t,out string errorMsg) where T : new()
  175. {
  176. errorMsg = string.Empty;
  177. if (_row[property.Name] is DBNull) return _t;
  178. try
  179. {
  180. var type = property.PropertyType;
  181. if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  182. {
  183. var nc = new NullableConverter(type);
  184. if (nc.UnderlyingType == _row.Table.Columns[property.Name].DataType)
  185. {
  186. property.SetValue(_t, _row[property.Name], null);
  187. return _t;
  188. }
  189. type = nc.UnderlyingType;
  190. }
  191. else
  192. {
  193. if (type != typeof(string) && type == _row.Table.Columns[property.Name].DataType)
  194. {
  195. property.SetValue(_t, _row[property.Name], null);
  196. return _t;
  197. }
  198. }
  199. if (type == typeof(bool))
  200. property.SetValue(_t, FuncStr.NullToInt(_row[property.Name]) == 1, null);
  201. else if (type.IsEnum)
  202. property.SetValue(_t, Enum.Parse(type, _row[property.Name].ToString()), null);
  203. else if (type.IsValueType)
  204. {
  205. List<Type> typelist = new List<Type>() { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) };
  206. var _value = new object();
  207. if (typelist.Any(t => t == type))
  208. {
  209. var va = new object[] { _row[property.Name].ToString(), System.Globalization.NumberStyles.Float, null, null };
  210. var result = type.InvokeMember("TryParse", System.Reflection.BindingFlags.InvokeMethod, null, null, va);
  211. if ((bool)result == true)
  212. {
  213. _value = va[3];
  214. }
  215. else
  216. {
  217. throw new Exception();
  218. }
  219. }
  220. else
  221. {
  222. _value = type.InvokeMember("Parse", System.Reflection.BindingFlags.InvokeMethod, null, null, new object[] { _row[property.Name].ToString() });
  223. }
  224. property.SetValue(_t, _value, null);
  225. }
  226. else if (type == typeof(string))
  227. property.SetValue(_t, _row[property.Name].ToString().Trim(), null);
  228. else property.SetValue(_t, _row[property.Name], null);
  229. return _t;
  230. }
  231. catch (Exception ex)
  232. {
  233. errorMsg= string.Format("{0}:{1}[{2}] Value is {3}, 值转换错误!", typeof(T).Name, property.Name, property.PropertyType, _row[property.Name]);
  234. return default(T);
  235. }
  236. }
  237. }
  238. }