Mapping.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Newtonsoft.Json;
  2. using System;
  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 IoTIntegrationPlatform.Common
  10. {
  11. /// <summary>
  12. /// 实体映射方法
  13. /// </summary>
  14. public static class Mapping
  15. {
  16. #region datareader向实体映射
  17. /// <summary>
  18. /// DataReader转泛型
  19. /// </summary>
  20. /// <typeparam name="T">传入的实体类</typeparam>
  21. /// <param name="objReader">DataReader对象</param>
  22. /// <returns></returns>
  23. public static IList<T> ReaderToList<T>(this IDataReader objReader)
  24. {
  25. using (objReader)
  26. {
  27. List<T> list = new List<T>();
  28. //获取传入的数据类型
  29. Type modelType = typeof(T);
  30. //遍历DataReader对象
  31. while (objReader.Read())
  32. {
  33. //使用与指定参数匹配最高的构造函数,来创建指定类型的实例
  34. T model = Activator.CreateInstance<T>();
  35. for (int i = 0; i < objReader.FieldCount; i++)
  36. {
  37. //判断字段值是否为空或不存在的值
  38. if (!IsNullOrDBNull(objReader[i]))
  39. {
  40. //匹配字段名
  41. PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  42. if (pi != null)
  43. {
  44. //绑定实体对象中同名的字段
  45. pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
  46. }
  47. }
  48. }
  49. list.Add(model);
  50. }
  51. return list;
  52. }
  53. }
  54. /// <summary>
  55. /// 对可空类型进行判断转换(*要不然会报错)
  56. /// </summary>
  57. /// <param name="value">DataReader字段的值</param>
  58. /// <param name="conversionType">该字段的类型</param>
  59. /// <returns></returns>
  60. private static object CheckType(object value, Type conversionType)
  61. {
  62. if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  63. {
  64. if (value == null)
  65. return "";
  66. System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
  67. conversionType = nullableConverter.UnderlyingType;
  68. }
  69. return Convert.ChangeType(value, conversionType);
  70. }
  71. /// <summary>
  72. /// 判断指定对象是否是有效值
  73. /// </summary>
  74. /// <param name="obj"></param>
  75. /// <returns></returns>
  76. private static bool IsNullOrDBNull(object obj)
  77. {
  78. return (obj == null || (obj is DBNull)) ? true : false;
  79. }
  80. /// <summary>
  81. /// DataReader转模型
  82. /// </summary>
  83. /// <typeparam name="T"></typeparam>
  84. /// <param name="objReader"></param>
  85. /// <returns></returns>
  86. public static T ReaderToModel<T>(this IDataReader objReader)
  87. {
  88. using (objReader)
  89. {
  90. if (objReader.Read())
  91. {
  92. Type modelType = typeof(T);
  93. int count = objReader.FieldCount;
  94. T model = Activator.CreateInstance<T>();
  95. for (int i = 0; i < count; i++)
  96. {
  97. if (!IsNullOrDBNull(objReader[i]))
  98. {
  99. PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  100. if (pi != null)
  101. {
  102. pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
  103. }
  104. }
  105. }
  106. return model;
  107. }
  108. }
  109. return default(T);
  110. }
  111. /// <summary>
  112. /// 实体数据映射 AB实体相互映射数据
  113. /// </summary>
  114. /// <typeparam name="R">目标实体</typeparam>
  115. /// <typeparam name="T">数据源实体</typeparam>
  116. /// <param name="model">数据源</param>
  117. /// <returns></returns>
  118. public static R EntityMapping<R, T>(T model)
  119. {
  120. R result = Activator.CreateInstance<R>();
  121. foreach (PropertyInfo info in typeof(R).GetProperties())
  122. {
  123. PropertyInfo pro = typeof(T).GetProperty(info.Name);
  124. if (pro != null)
  125. {
  126. info.SetValue(result, pro.GetValue(model));
  127. }
  128. }
  129. return result;
  130. }
  131. /// <summary>
  132. /// Datatable 实体转换为实体
  133. /// </summary>
  134. /// <typeparam name="T"></typeparam>
  135. /// <param name="dt"></param>
  136. /// <returns></returns>
  137. public static T DataTableToModel<T>(DataTable dt)
  138. {
  139. // 定义实体
  140. T t = Activator.CreateInstance<T>();
  141. // 获得此模型的类型
  142. Type type = typeof(T);
  143. foreach (DataRow dr in dt.Rows)
  144. {
  145. // 获得此模型的公共属性
  146. PropertyInfo[] propertys = t.GetType().GetProperties();
  147. foreach (PropertyInfo pi in propertys)
  148. {
  149. PropertyInfo[] propertys1 = pi.GetType().GetProperties();
  150. var attribute = pi.GetCustomAttributes(typeof(JsonPropertyAttribute), false).FirstOrDefault();
  151. string proname = ((JsonPropertyAttribute)attribute).PropertyName;
  152. if (dt.Columns.Contains(proname))
  153. {
  154. // 判断此属性是否有Setter
  155. if (!pi.CanWrite) continue;
  156. object value = dr[proname];
  157. if (value != DBNull.Value)
  158. {
  159. pi.SetValue(t, CheckType(value, pi.PropertyType), null);
  160. }
  161. }
  162. }
  163. break;
  164. }
  165. return t;
  166. }
  167. /// <summary>
  168. /// 模型转Json字符串
  169. /// </summary>
  170. /// <typeparam name="T"></typeparam>
  171. /// <returns></returns>
  172. public static string ModeltoJson<T>(T Model)
  173. {
  174. string json = string.Empty;
  175. // 定义实体
  176. T t = Activator.CreateInstance<T>();
  177. // 获得此模型的类型
  178. Type type = typeof(T);
  179. // 获得此模型的公共属性
  180. PropertyInfo[] propertys = t.GetType().GetProperties();
  181. PropertyInfo[] Mpropertys = Model.GetType().GetProperties();
  182. foreach (PropertyInfo pi in propertys)
  183. {
  184. json += "" + pi.Name + ":" + "";
  185. }
  186. return json;
  187. }
  188. #endregion
  189. }
  190. }