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
{
///
/// 实体扩展
///
public static class EntityExtensions
{
#region 实体名称
///
/// 实体名称
///
///
///
public static string GetName(this T entity)
{
return typeof(T).Name;
}
#endregion
#region 实体克隆
///
/// 单一实体克隆
///
///
///
///
public static T Clone(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;
}
///
/// 反射克隆
///
///
///
///
public static List CloneReflect(this List list) where T : new()
{
List items = new List();
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;
}
///
/// Newtonsoft克隆
///
///
///
///
public static List CloneNewtonsoft(this List list) where T : new()
{
var str = JsonConvert.SerializeObject(list);
return JsonConvert.DeserializeObject>(str);
}
///
/// BinaryFormatter克隆
///
///
///
///
public static List CloneBinaryFormatter(this List list)
{
using (Stream objectStream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, list);
objectStream.Seek(0, SeekOrigin.Begin);
return (List)formatter.Deserialize(objectStream);
}
}
#endregion
#region 实体拷贝
///
/// 反射实现两个类的对象之间相同属性的值的复制 适用于初始化新实体
///
/// 返回的实体
/// 数据源实体
/// 数据源实体
/// 返回的新实体
public static TResult Mapper(this TEntity source)
{
var d = Activator.CreateInstance();
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;
}
///
/// 反射实现两个类的对象之间相同属性的值的复制 适用于没有新建实体之间
///
/// 返回的实体
/// 数据源实体
/// 返回的实体
/// 数据源实体
///
public static TResult MapperToModel(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;
}
///
/// 反射实现实体类清空属性值,适用与批量更新数据,只更新部分字段
///
///
///
///
///
public static List SetEmpty(this List 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;
}
}
///
/// 追加实体对象
///
///
///
///
///
public static List AddGet(this List source,TEntity entity) where TEntity : class
{
source.Add(entity);
return source;
}
#endregion
}
}