ModelConvertHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace WCS_Client.Utility
  10. {
  11. /// <summary>
  12. /// 实体转换辅助类
  13. /// </summary>
  14. public class ModelConvertHelper<T> where T : new()
  15. {
  16. public static IList<T> ConvertToModel(DataTable dt)
  17. {
  18. // 定义集合
  19. IList<T> ts = new List<T>();
  20. // 获得此模型的类型
  21. Type type = typeof(T);
  22. string tempName = "";
  23. foreach (DataRow dr in dt.Rows)
  24. {
  25. T t = new T();
  26. // 获得此模型的公共属性
  27. PropertyInfo[] propertys = t.GetType().GetProperties();
  28. foreach (PropertyInfo pi in propertys)
  29. {
  30. tempName = pi.Name; // 检查DataTable是否包含此列
  31. if (dt.Columns.Contains(tempName))
  32. {
  33. // 判断此属性是否有Setter
  34. if (!pi.CanWrite) continue;
  35. object value = dr[tempName];
  36. if (value != DBNull.Value)
  37. pi.SetValue(t, value, null);
  38. }
  39. }
  40. ts.Add(t);
  41. }
  42. return ts;
  43. }
  44. }
  45. /// <summary>
  46. /// Datatble转list
  47. /// </summary>
  48. public class DatatableConvertHelper
  49. {
  50. /// <summary>
  51. /// Convert a List{T} to a DataTable.
  52. /// </summary>
  53. public static DataTable ToDataTable<T>(List<T> items)
  54. {
  55. var tb = new DataTable(typeof(T).Name);
  56. PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  57. foreach (PropertyInfo prop in props)
  58. {
  59. Type t = GetCoreType(prop.PropertyType);
  60. tb.Columns.Add(prop.Name, t);
  61. }
  62. foreach (T item in items)
  63. {
  64. var values = new object[props.Length];
  65. for (int i = 0; i < props.Length; i++)
  66. {
  67. values[i] = props[i].GetValue(item, null);
  68. }
  69. tb.Rows.Add(values);
  70. }
  71. return tb;
  72. }
  73. /// <summary>
  74. /// Determine of specified type is nullable
  75. /// </summary>
  76. private static bool IsNullable(Type t)
  77. {
  78. return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
  79. }
  80. /// <summary>
  81. /// Return underlying type if type is Nullable otherwise return the type
  82. /// </summary>
  83. private static Type GetCoreType(Type t)
  84. {
  85. if (t != null && IsNullable(t))
  86. {
  87. if (!t.IsValueType)
  88. {
  89. return t;
  90. }
  91. else
  92. {
  93. return Nullable.GetUnderlyingType(t);
  94. }
  95. }
  96. else
  97. {
  98. return t;
  99. }
  100. }
  101. public static DataTable ToDataTable<T>(IEnumerable<T> collection)
  102. {
  103. var props = typeof(T).GetProperties();
  104. var dt = new DataTable();
  105. dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
  106. if (collection.Count() > 0)
  107. {
  108. for (int i = 0; i < collection.Count(); i++)
  109. {
  110. ArrayList tempList = new ArrayList();
  111. foreach (PropertyInfo pi in props)
  112. {
  113. object obj = pi.GetValue(collection.ElementAt(i), null);
  114. tempList.Add(obj);
  115. }
  116. object[] array = tempList.ToArray();
  117. dt.LoadDataRow(array, true);
  118. }
  119. }
  120. return dt;
  121. }
  122. }
  123. }