123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Serialization;
- namespace NXWMS.Code.Extends
- {
- /// <summary>
- /// 实体扩展
- /// </summary>
- public static class EntityExtensions
- {
- #region 实体名称
- /// <summary>
- /// 实体名称
- /// </summary>
- /// <param name="operationStatusInfo"></param>
- /// <returns></returns>
- public static string GetName<T>(this T entity)
- {
- return typeof(T).Name;
- }
- #endregion
- #region 实体克隆
- /// <summary>
- /// 单一实体克隆
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static T Clone<T>(this T obj)
- {
- object retval;
- using (MemoryStream ms = new MemoryStream())
- {
- XmlSerializer xml = new XmlSerializer(typeof(T));
- xml.Serialize(ms, obj);
- ms.Seek(0, SeekOrigin.Begin);
- retval = xml.Deserialize(ms);
- ms.Close();
- }
- return (T)retval;
- }
- /// <summary>
- /// 反射克隆
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- /// <returns></returns>
- public static List<T> CloneReflect<T>(this List<T> list) where T : new()
- {
- List<T> items = new List<T>();
- foreach (var m in list)
- {
- var model = new T();
- var ps = model.GetType().GetProperties();
- var properties = m.GetType().GetProperties();
- foreach (var p in properties)
- {
- foreach (var pm in ps)
- {
- if (pm.Name == p.Name)
- {
- pm.SetValue(model, p.GetValue(m, null), null);
- }
- }
- }
- items.Add(model);
- }
- return items;
- }
- /// <summary>
- /// Newtonsoft克隆
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- /// <returns></returns>
- public static List<T> CloneNewtonsoft<T>(this List<T> list) where T : new()
- {
- var str = JsonConvert.SerializeObject(list);
- return JsonConvert.DeserializeObject<List<T>>(str);
- }
- /// <summary>
- /// BinaryFormatter克隆
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- /// <returns></returns>
- public static List<T> CloneBinaryFormatter<T>(this List<T> list)
- {
- using (Stream objectStream = new MemoryStream())
- {
- IFormatter formatter = new BinaryFormatter();
- formatter.Serialize(objectStream, list);
- objectStream.Seek(0, SeekOrigin.Begin);
- return (List<T>)formatter.Deserialize(objectStream);
- }
- }
- #endregion
- #region 实体拷贝
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制 适用于初始化新实体
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="s">数据源实体</param>
- /// <returns>返回的新实体</returns>
- public static TResult Mapper<TResult, TEntity>(this TEntity source)
- {
- var d = Activator.CreateInstance<TResult>();
- try
- {
- var Types = source.GetType();
- var Typed = typeof(TResult);
- foreach (PropertyInfo sp in Types.GetProperties())
- {
- foreach (PropertyInfo dp in Typed.GetProperties())
- {
- if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")
- {
- dp.SetValue(d, sp.GetValue(source, null), null);
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return d;
- }
- /// <summary>
- /// 反射实现两个类的对象之间相同属性的值的复制 适用于没有新建实体之间
- /// </summary>
- /// <typeparam name="D">返回的实体</typeparam>
- /// <typeparam name="S">数据源实体</typeparam>
- /// <param name="d">返回的实体</param>
- /// <param name="s">数据源实体</param>
- /// <returns></returns>
- public static TResult MapperToModel<TResult, TEntity>(this TResult result, TEntity source)
- {
- try
- {
- var Types = source.GetType();
- var Typed = typeof(TResult);
- foreach (PropertyInfo sp in Types.GetProperties())
- {
- foreach (PropertyInfo dp in Typed.GetProperties())
- {
- if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType)
- {
- dp.SetValue(result, sp.GetValue(source, null), null);
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return result;
- }
- /// <summary>
- /// 反射实现实体类清空属性值,适用与批量更新数据,只更新部分字段
- /// </summary>
- /// <typeparam name="TEntity"></typeparam>
- /// <param name="source"></param>
- /// <param name="fieldName"></param>
- /// <returns></returns>
- public static List<TEntity> SetEmpty<TEntity>(this List<TEntity> source,string[] fieldNames) where TEntity : class
- {
- try
- {
- foreach(var item in source)
- {
- var Types = item.GetType();
- foreach (PropertyInfo dp in Types.GetProperties())
- {
- if (!fieldNames.Where(s=>s.ToLower() == dp.Name.ToLower()).Any())
- {
- dp.SetValue(item, null, null);
- }
- }
- }
- return source;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 追加实体对象
- /// </summary>
- /// <typeparam name="TEntity"></typeparam>
- /// <param name="source"></param>
- /// <param name="entity"></param>
- /// <returns></returns>
- public static List<TEntity> AddGet<TEntity>(this List<TEntity> source,TEntity entity) where TEntity : class
- {
- source.Add(entity);
- return source;
- }
- #endregion
- }
- }
|