using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace System { /// /// 对象属性值复制 /// public class ObjectCopyHelper { /// /// 属性映射(静态对象,无需重复建立属性映射关系,提高效率) /// public static Dictionary> MapDic = new Dictionary>(); /// /// S复制到D(创建对象D) /// /// 输出对象类型 /// 输入对象类型 /// 输入对象 /// public static D Copy(S s) where D : class, new() where S : class, new() { if (s == null) { return default(D); } //使用无参数构造函数,创建指定泛型类型参数所指定类型的实例 D d = Activator.CreateInstance(); return Copy(s, d); } /// /// S复制到D(对象D已存在) /// /// 输出对象类型 /// 输入对象类型 /// 输入对象 /// 输出对象 /// public static D Copy(S s, D d) where D : class, new() where S : class, new() { if (s == null || d == null) { return d; } try { var sType = s.GetType(); var dType = typeof(D); //属性映射Key string mapkey = dType.FullName + "_" + sType.FullName; if (MapDic.ContainsKey(mapkey)) { //已存在属性映射 foreach (var item in MapDic[mapkey]) { //按照属性映射关系赋值 //.net 4 //dType.GetProperty(item).SetValue(d, sType.GetProperty(item).GetValue(s, null), null); //.net 4.5 dType.GetProperty(item).SetValue(d, sType.GetProperty(item).GetValue(s)); } } else { //不存在属性映射,需要建立属性映射 List namelist = new List(); Dictionary dic = new Dictionary(); //遍历获取输入类型的属性(属性名称,类型,值) foreach (PropertyInfo sP in sType.GetProperties()) { //.net 4 //dic.Add(sP.Name, new TypeAndValue() { type = sP.PropertyType, value = sP.GetValue(s, null) }); //.net 4.5 dic.Add(sP.Name, new TypeAndValue() { type = sP.PropertyType, value = sP.GetValue(s) }); } //遍历输出类型的属性,并与输入类型(相同名称和类型的属性)建立映射,并赋值 foreach (PropertyInfo dP in dType.GetProperties()) { if (dic.Keys.Contains(dP.Name)) { if (dP.PropertyType == dic[dP.Name].type) { namelist.Add(dP.Name); //.net 4 //dP.SetValue(d, dic[dP.Name].value, null); //.net 4.5 dP.SetValue(d, dic[dP.Name].value); } } } //保存映射 if (!MapDic.ContainsKey(mapkey)) { MapDic.Add(mapkey, namelist); } } } catch (Exception ex) { throw ex; } return d; } /// /// SList复制到DList /// /// 输出对象类型 /// 输入对象类型 /// 输入对象集合 /// public static IQueryable Copy(IQueryable sList) where D : class, new() where S : class, new() { List dList = new List(); foreach (var item in sList) { dList.Add(Copy(item)); } return dList.AsQueryable(); } } /// /// 类型和值 /// class TypeAndValue { /// /// 类型 /// public Type type { get; set; } /// /// 值 /// public object value { get; set; } } }