ModelHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using System.Reflection;
  4. namespace Houdar.Core.Util.Common
  5. {
  6. /// <summary>
  7. /// DataTable与实体类互相转换
  8. /// </summary>
  9. /// <typeparam name="T">实体类</typeparam>
  10. public class ModelHandler<T> where T : new()
  11. {
  12. #region DataTable转换成实体类
  13. /// <summary>
  14. /// 填充对象列表:用DataSet的第一个表填充实体类
  15. /// </summary>
  16. /// <param name="ds">DataSet</param>
  17. /// <returns></returns>
  18. public List<T> FillModel(DataSet ds)
  19. {
  20. if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
  21. {
  22. return null;
  23. }
  24. else
  25. {
  26. return FillModel(ds.Tables[0]);
  27. }
  28. }
  29. /// <summary>
  30. /// 填充对象列表:用DataSet的第index个表填充实体类
  31. /// </summary>
  32. public List<T> FillModel(DataSet ds, int index)
  33. {
  34. if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
  35. {
  36. return null;
  37. }
  38. else
  39. {
  40. return FillModel(ds.Tables[index]);
  41. }
  42. }
  43. /// <summary>
  44. /// 填充对象列表:用DataTable填充实体类
  45. /// </summary>
  46. public List<T> FillModel(DataTable dt)
  47. {
  48. if (dt == null || dt.Rows.Count == 0)
  49. {
  50. return null;
  51. }
  52. List<T> modelList = new List<T>();
  53. foreach (DataRow dr in dt.Rows)
  54. {
  55. //T model = (T)Activator.CreateInstance(typeof(T));
  56. T model = new T();
  57. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  58. {
  59. model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
  60. }
  61. modelList.Add(model);
  62. }
  63. return modelList;
  64. }
  65. /// <summary>
  66. /// 填充对象:用DataRow填充实体类
  67. /// </summary>
  68. public T FillModel(DataRow dr)
  69. {
  70. if (dr == null)
  71. {
  72. return default(T);
  73. }
  74. //T model = (T)Activator.CreateInstance(typeof(T));
  75. T model = new T();
  76. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  77. {
  78. model.GetType().GetProperty(propertyInfo.Name).SetValue(model, dr[propertyInfo.Name], null);
  79. }
  80. return model;
  81. }
  82. #endregion
  83. #region 实体类转换成DataTable
  84. /// <summary>
  85. /// 实体类转换成DataSet
  86. /// </summary>
  87. /// <param name="modelList">实体类列表</param>
  88. /// <returns></returns>
  89. public DataSet FillDataSet(List<T> modelList)
  90. {
  91. if (modelList == null || modelList.Count == 0)
  92. {
  93. return null;
  94. }
  95. else
  96. {
  97. DataSet ds = new DataSet();
  98. ds.Tables.Add(FillDataTable(modelList));
  99. return ds;
  100. }
  101. }
  102. /// <summary>
  103. /// 实体类转换成DataTable
  104. /// </summary>
  105. /// <param name="modelList">实体类列表</param>
  106. /// <returns></returns>
  107. public DataTable FillDataTable(List<T> modelList)
  108. {
  109. if (modelList == null || modelList.Count == 0)
  110. {
  111. return null;
  112. }
  113. DataTable dt = CreateData(modelList[0]);
  114. foreach (T model in modelList)
  115. {
  116. DataRow dataRow = dt.NewRow();
  117. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  118. {
  119. dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
  120. }
  121. dt.Rows.Add(dataRow);
  122. }
  123. return dt;
  124. }
  125. /// <summary>
  126. /// 根据实体类得到表结构
  127. /// </summary>
  128. /// <param name="model">实体类</param>
  129. /// <returns></returns>
  130. private DataTable CreateData(T model)
  131. {
  132. DataTable dataTable = new DataTable(typeof(T).Name);
  133. foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
  134. {
  135. dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
  136. }
  137. return dataTable;
  138. }
  139. #endregion
  140. }
  141. }