ModelHandler.cs 4.6 KB

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