/// <summary> /// 拷贝列表 /// </summary> /// <typeparam name="S">源对象类型</typeparam> /// <typeparam name="T">目标对象类型</typeparam> /// <param name="lst">源对像列表</param> /// <param name="option">拷贝选项</param> /// <returns>返回目标对象列表</returns> public static IEnumerable <T> CopyList <S, T>(this IEnumerable <S> lst, ObjectCopyOption option = null) where T : new() { if (lst == null) { yield break; } if (lst.Count() == 0) { yield break; } foreach (S item in lst) { yield return(item.Copy <T>(option)); } }
private static IEnumerable <string> GetNeedCopyField(ObjectCopyOption option, Type targetType) { if (targetType == null) { return(null); } var tp = targetType.GetProperties(); if (tp == null || tp.Length == 0) { return(null); } if (option == null) { //表示返回全部属性 return((from item in tp select item.Name).ToList()); } if (option.NeedFields != null && option.NeedFields.Count() > 0) { //拷贝指定的字段 return(option.NeedFields); } if (option.IgnoreFields != null && option.IgnoreFields.Count() > 0) { //不拷贝的字段 var re = new List <string>(); foreach (PropertyInfo item in tp) { if (!option.IgnoreFields.Contains(item.Name)) { re.Add(item.Name); } } return(re); } //返回全部字段 return((from item in tp select item.Name).ToList()); }
/// <summary> /// 对象拷贝 /// </summary> /// <typeparam name="S">源对象类型</typeparam> /// <typeparam name="T">目标对象类型</typeparam> /// <param name="source">源对象</param> /// <param name="option">拷贝选项(不传此项将使用默认拷贝规则)</param> /// <returns></returns> public static T Copy <T>(this object source, ObjectCopyOption option = null) where T : new() { if (source == null) { return(default(T)); } Type tType = typeof(T); //从目标类型中确定要复制的字段 var needCopyField = GetNeedCopyField(option, tType); if (needCopyField == null || needCopyField.Count() == 0) { return(default(T)); } else { T re = new T(); Type sType = source.GetType(); foreach (string pName in needCopyField) { var tp = tType.GetProperty(pName); var sp = sType.GetProperty(MapFieldName(pName, (option == null ? null : option.FiledNameMaps))); if (tp != null && sp != null && tp.CanWrite && sp.CanRead) { var tpt = tp.PropertyType; var spt = sp.PropertyType; if (tpt == spt)//类型相同 { tp.SetValue(re, sp.GetValue(source)); } else { if (tpt.IsGenericType && !spt.IsGenericType && tpt.GetGenericArguments()[0] == spt)//原始类型转泛型 { tp.SetValue(re, sp.GetValue(source)); } else if (spt.IsGenericType && !tpt.IsGenericType && spt.GetGenericArguments()[0] == tpt)//泛型转原始类型 { object so = sp.GetValue(source, null); if (so != null) { tp.SetValue(re, Convert.ChangeType(so, tpt)); //object hasVal = so.GetType().InvokeMember("HasValue", BindingFlags.Default, null, null, null); //if (Convert.ToBoolean(hasVal)) //{ // object val = so.GetType().InvokeMember("Value", BindingFlags.Default, null, null, null); // tp.SetValue(re, val); //} } } else if (tpt == typeof(string))//直接转字符串 { tp.SetValue(re, sp.GetValue(source).ToString()); } } } } //触发行拷贝动作 if (option != null && option.ItemCopyAction != null) { option.ItemCopyAction(re, option.ItemCopyActionParam); } return(re); } }