EntityExtensions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Runtime.Serialization;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Xml.Serialization;
  13. namespace NXWMS.Code.Extends
  14. {
  15. /// <summary>
  16. /// 实体扩展
  17. /// </summary>
  18. public static class EntityExtensions
  19. {
  20. #region 实体名称
  21. /// <summary>
  22. /// 实体名称
  23. /// </summary>
  24. /// <param name="operationStatusInfo"></param>
  25. /// <returns></returns>
  26. public static string GetName<T>(this T entity)
  27. {
  28. return typeof(T).Name;
  29. }
  30. #endregion
  31. #region 实体克隆
  32. /// <summary>
  33. /// 单一实体克隆
  34. /// </summary>
  35. /// <typeparam name="T"></typeparam>
  36. /// <param name="obj"></param>
  37. /// <returns></returns>
  38. public static T Clone<T>(this T obj)
  39. {
  40. object retval;
  41. using (MemoryStream ms = new MemoryStream())
  42. {
  43. XmlSerializer xml = new XmlSerializer(typeof(T));
  44. xml.Serialize(ms, obj);
  45. ms.Seek(0, SeekOrigin.Begin);
  46. retval = xml.Deserialize(ms);
  47. ms.Close();
  48. }
  49. return (T)retval;
  50. }
  51. /// <summary>
  52. /// 反射克隆
  53. /// </summary>
  54. /// <typeparam name="T"></typeparam>
  55. /// <param name="list"></param>
  56. /// <returns></returns>
  57. public static List<T> CloneReflect<T>(this List<T> list) where T : new()
  58. {
  59. List<T> items = new List<T>();
  60. foreach (var m in list)
  61. {
  62. var model = new T();
  63. var ps = model.GetType().GetProperties();
  64. var properties = m.GetType().GetProperties();
  65. foreach (var p in properties)
  66. {
  67. foreach (var pm in ps)
  68. {
  69. if (pm.Name == p.Name)
  70. {
  71. pm.SetValue(model, p.GetValue(m, null), null);
  72. }
  73. }
  74. }
  75. items.Add(model);
  76. }
  77. return items;
  78. }
  79. /// <summary>
  80. /// Newtonsoft克隆
  81. /// </summary>
  82. /// <typeparam name="T"></typeparam>
  83. /// <param name="list"></param>
  84. /// <returns></returns>
  85. public static List<T> CloneNewtonsoft<T>(this List<T> list) where T : new()
  86. {
  87. var str = JsonConvert.SerializeObject(list);
  88. return JsonConvert.DeserializeObject<List<T>>(str);
  89. }
  90. /// <summary>
  91. /// BinaryFormatter克隆
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <param name="list"></param>
  95. /// <returns></returns>
  96. public static List<T> CloneBinaryFormatter<T>(this List<T> list)
  97. {
  98. using (Stream objectStream = new MemoryStream())
  99. {
  100. IFormatter formatter = new BinaryFormatter();
  101. formatter.Serialize(objectStream, list);
  102. objectStream.Seek(0, SeekOrigin.Begin);
  103. return (List<T>)formatter.Deserialize(objectStream);
  104. }
  105. }
  106. #endregion
  107. #region 实体拷贝
  108. /// <summary>
  109. /// 反射实现两个类的对象之间相同属性的值的复制 适用于初始化新实体
  110. /// </summary>
  111. /// <typeparam name="D">返回的实体</typeparam>
  112. /// <typeparam name="S">数据源实体</typeparam>
  113. /// <param name="s">数据源实体</param>
  114. /// <returns>返回的新实体</returns>
  115. public static TResult Mapper<TResult, TEntity>(this TEntity source)
  116. {
  117. var d = Activator.CreateInstance<TResult>();
  118. try
  119. {
  120. var Types = source.GetType();
  121. var Typed = typeof(TResult);
  122. foreach (PropertyInfo sp in Types.GetProperties())
  123. {
  124. foreach (PropertyInfo dp in Typed.GetProperties())
  125. {
  126. if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")
  127. {
  128. dp.SetValue(d, sp.GetValue(source, null), null);
  129. }
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. throw ex;
  136. }
  137. return d;
  138. }
  139. /// <summary>
  140. /// 反射实现两个类的对象之间相同属性的值的复制 适用于没有新建实体之间
  141. /// </summary>
  142. /// <typeparam name="D">返回的实体</typeparam>
  143. /// <typeparam name="S">数据源实体</typeparam>
  144. /// <param name="d">返回的实体</param>
  145. /// <param name="s">数据源实体</param>
  146. /// <returns></returns>
  147. public static TResult MapperToModel<TResult, TEntity>(this TResult result, TEntity source)
  148. {
  149. try
  150. {
  151. var Types = source.GetType();
  152. var Typed = typeof(TResult);
  153. foreach (PropertyInfo sp in Types.GetProperties())
  154. {
  155. foreach (PropertyInfo dp in Typed.GetProperties())
  156. {
  157. if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType)
  158. {
  159. dp.SetValue(result, sp.GetValue(source, null), null);
  160. }
  161. }
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. throw ex;
  167. }
  168. return result;
  169. }
  170. /// <summary>
  171. /// 反射实现实体类清空属性值,适用与批量更新数据,只更新部分字段
  172. /// </summary>
  173. /// <typeparam name="TEntity"></typeparam>
  174. /// <param name="source"></param>
  175. /// <param name="fieldName"></param>
  176. /// <returns></returns>
  177. public static List<TEntity> SetEmpty<TEntity>(this List<TEntity> source,string[] fieldNames) where TEntity : class
  178. {
  179. try
  180. {
  181. foreach(var item in source)
  182. {
  183. var Types = item.GetType();
  184. foreach (PropertyInfo dp in Types.GetProperties())
  185. {
  186. if (!fieldNames.Where(s=>s.ToLower() == dp.Name.ToLower()).Any())
  187. {
  188. dp.SetValue(item, null, null);
  189. }
  190. }
  191. }
  192. return source;
  193. }
  194. catch (Exception ex)
  195. {
  196. throw ex;
  197. }
  198. }
  199. /// <summary>
  200. /// 追加实体对象
  201. /// </summary>
  202. /// <typeparam name="TEntity"></typeparam>
  203. /// <param name="source"></param>
  204. /// <param name="entity"></param>
  205. /// <returns></returns>
  206. public static List<TEntity> AddGet<TEntity>(this List<TEntity> source,TEntity entity) where TEntity : class
  207. {
  208. source.Add(entity);
  209. return source;
  210. }
  211. #endregion
  212. }
  213. }