颐和api

ModelConverterHelper.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace MadRunFabric.Common
  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. tempName = pi.Name; // 检查DataTable是否包含此列
  30. //if (dt.Columns.Contains(tempName))
  31. //{
  32. // 判断此属性是否有Setter
  33. if (!pi.CanWrite) continue;
  34. object value = dr[i];//object value = dr[tempName];
  35. if (pi.PropertyType.Equals(typeof(System.Int64)))
  36. { pi.SetValue(t, Convert.ToInt64(value), null); }
  37. else if (pi.PropertyType.Equals(typeof(System.Int32)))
  38. { pi.SetValue(t, Convert.ToInt32(value), null); }
  39. else if (pi.PropertyType.Equals(typeof(System.Decimal)))
  40. { pi.SetValue(t, Convert.ToDecimal(value), null); }
  41. else if (pi.PropertyType.Equals(typeof(System.DateTime)))
  42. {
  43. if (value != DBNull.Value && value.ToString() != "")
  44. pi.SetValue(t, Convert.ToDateTime(value), null);
  45. else
  46. pi.SetValue(t, DateTime.Now, null);
  47. }
  48. else
  49. if (value != DBNull.Value)
  50. pi.SetValue(t, value, null);
  51. i++;
  52. //}
  53. }
  54. ts.Add(t);
  55. }
  56. return ts;
  57. }
  58. // **//// <summary>
  59. /// 转换IList<T>为List<T> //将IList接口泛型转为List泛型类型
  60. /// </summary>
  61. /// <typeparam name="T">指定的集合中泛型的类型</typeparam>
  62. /// <param name="gbList">需要转换的IList</param>
  63. /// <returns></returns>
  64. public static List<T> ConvertIListToList<T>(IList<T> gbList) where T : class //静态方法,泛型转换,
  65. {
  66. if (gbList != null && gbList.Count >= 1)
  67. {
  68. List<T> list = new List<T>();
  69. for (int i = 0; i < gbList.Count; i++) //将IList中的元素复制到List中
  70. {
  71. T temp = gbList[i] as T;
  72. if (temp != null)
  73. list.Add(temp);
  74. }
  75. return list;
  76. }
  77. return null;
  78. }
  79. /// <summary>
  80. /// 把DataTable转换成指定类型的List
  81. /// </summary>
  82. /// <param name="dt"></param>
  83. /// <returns></returns>
  84. public static IList<T> ConvertDataTableToList(DataTable dt)
  85. {
  86. // 定义集合
  87. IList<T> ts = new List<T>();
  88. string tempName = "";
  89. foreach (DataRow dr in dt.Rows)
  90. {
  91. T t = new T();
  92. // 获得此模型的公共属性
  93. PropertyInfo[] propertys = t.GetType().GetProperties();
  94. foreach (PropertyInfo pi in propertys)
  95. {
  96. tempName = pi.Name; // 检查DataTable是否包含此列
  97. if (dt.Columns.Contains(tempName))
  98. {
  99. // 判断此属性是否有Setter
  100. if (!pi.CanWrite) continue;
  101. object value = dr[tempName];
  102. if (value != DBNull.Value)
  103. pi.SetValue(t, value, null);
  104. }
  105. }
  106. ts.Add(t);
  107. }
  108. return ts;
  109. }
  110. /// <summary>
  111. /// 把泛型List转换成DataTable
  112. /// </summary>
  113. /// <param name="list"></param>
  114. /// <returns></returns>
  115. public static DataTable ConvertListToDataTable(List<T> list)
  116. {
  117. DataTable dt = new DataTable();
  118. // 获得此模型的公共属性
  119. PropertyInfo[] propertys = typeof(T).GetProperties();
  120. foreach (PropertyInfo pi in propertys)
  121. {
  122. // 判断此属性是否有Getter
  123. if (!pi.CanRead) continue;
  124. dt.Columns.Add(pi.Name, pi.PropertyType);
  125. }
  126. foreach (T item in list)
  127. {
  128. propertys = item.GetType().GetProperties();
  129. DataRow newRow = dt.NewRow();
  130. foreach (PropertyInfo pi in propertys)
  131. {
  132. if (!pi.CanRead) continue;
  133. newRow[pi.Name] = pi.GetValue(item);
  134. }
  135. dt.Rows.Add(newRow);
  136. }
  137. return dt;
  138. }
  139. /// <summary>
  140. /// List集合转DataTable
  141. /// </summary>
  142. /// <typeparam name="T">实体类型</typeparam>
  143. /// <param name="list">传入集合</param>
  144. /// <param name="isStoreDB">是否存入数据库DateTime字段,date时间范围没事,取出展示不用设置TRUE</param>
  145. /// <returns>返回datatable结果</returns>
  146. public static DataTable ListToTable<T>(List<T> list, params string[] propertyName)
  147. {
  148. List<string> propertyNameList = new List<string>();
  149. if (propertyName != null)
  150. {
  151. propertyNameList.AddRange(propertyName);
  152. }
  153. DataTable result = new DataTable();
  154. if (list.Count > 0)
  155. {
  156. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  157. foreach (PropertyInfo pi in propertys)
  158. {
  159. if (propertyNameList.Count == 0)
  160. {
  161. //if (DBNull.Value.Equals(pi.PropertyType))
  162. //{
  163. // // pi.PropertyType = DateTime;
  164. //}
  165. Type colType = pi.PropertyType;
  166. if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
  167. {
  168. colType = colType.GetGenericArguments()[0];
  169. }
  170. result.Columns.Add(pi.Name, colType);
  171. //result.Columns.Add(pi.Name, pi.PropertyType);
  172. }
  173. else
  174. {
  175. if (propertyNameList.Contains(pi.Name))
  176. {
  177. result.Columns.Add(pi.Name, pi.PropertyType);
  178. }
  179. }
  180. }
  181. for (int i = 0; i < list.Count; i++)
  182. {
  183. ArrayList tempList = new ArrayList();
  184. foreach (PropertyInfo pi in propertys)
  185. {
  186. if (propertyNameList.Count == 0)
  187. {
  188. object obj = pi.GetValue(list[i], null);
  189. tempList.Add(obj);
  190. }
  191. else
  192. {
  193. if (propertyNameList.Contains(pi.Name))
  194. {
  195. object obj = pi.GetValue(list[i], null);
  196. tempList.Add(obj);
  197. }
  198. }
  199. }
  200. object[] array = tempList.ToArray();
  201. result.LoadDataRow(array, true);
  202. }
  203. }
  204. return result;
  205. }
  206. }
  207. }