我认为我错过了一个带有valueinjecter和/或AutoMapper的简单概念,但是如何将父dto.Entity深深克隆到biz.Entity并包含所有孩子呢?
例如,biz.person.InjectFrom(dto.person)
。我希望dto.person.AddressList集合可以复制到biz.person.AddressList集合,即使dto.Address
和biz.Address
与类型不同,但具有相同的属性名称。
我的想法是,如果Parent属性名称拼写相同,例如AddressList,那么2个底层对象是否属于不同类型并不重要。它仍然会复制同名的简单类型,如int,string等。
谢谢
答案 0 :(得分:8)
当对象中的数组/列表具有相同的名称但不同的类型(即名为Animals类型为ORMAnimals []的属性映射到名为Animals类型为Animals [])的属性时,我遇到了同样的问题。
对Chuck Norris在深度克隆page上的示例代码进行了一些小调整我在测试代码中使用了它:
public class CloneInjection : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null;
}
protected override object SetValue(ConventionInfo c)
{
//for value types and string just return the value as is
if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)
|| c.TargetProp.Type.IsValueType || c.TargetProp.Type == typeof(string))
return c.SourceProp.Value;
//handle arrays
if (c.SourceProp.Type.IsArray)
{
var arr = c.SourceProp.Value as Array;
var clone = Activator.CreateInstance(c.TargetProp.Type, arr.Length) as Array;
for (int index = 0; index < arr.Length; index++)
{
var a = arr.GetValue(index);
if (a.GetType().IsValueType || a.GetType() == typeof(string)) continue;
clone.SetValue(Activator.CreateInstance(c.TargetProp.Type.GetElementType()).InjectFrom<CloneInjection>(a), index);
}
return clone;
}
if (c.SourceProp.Type.IsGenericType)
{
//handle IEnumerable<> also ICollection<> IList<> List<>
if (c.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
{
var t = c.TargetProp.Type.GetGenericArguments()[0];
if (t.IsValueType || t == typeof(string)) return c.SourceProp.Value;
var tlist = typeof(List<>).MakeGenericType(t);
var list = Activator.CreateInstance(tlist);
var addMethod = tlist.GetMethod("Add");
foreach (var o in c.SourceProp.Value as IEnumerable)
{
var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o);
addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e);
}
return list;
}
//unhandled generic type, you could also return null or throw
return c.SourceProp.Value;
}
//for simple object types create a new instace and apply the clone injection on it
return Activator.CreateInstance(c.TargetProp.Type)
.InjectFrom<CloneInjection>(c.SourceProp.Value);
}
}
答案 1 :(得分:1)
我遇到了这个问题,即使使用CloneInjection也无法复制具有相同名称和不同类型的属性。因此,我更改了CloneInjection中的一些内容(我使用的是ValueInjecter版本3.1.3)。
public class CloneInjection : LoopInjection
{
protected override void Execute(PropertyInfo sp, object source, object target)
{
var tp = target.GetType().GetProperty(sp.Name);
if (tp == null) return;
var val = sp.GetValue(source);
if (val == null) return;
tp.SetValue(target, GetClone(sp, tp, val));
}
private static object GetClone(PropertyInfo sp, PropertyInfo tp, object val)
{
if (sp.PropertyType.IsValueType || sp.PropertyType == typeof(string))
{
return val;
}
if (sp.PropertyType.IsArray)
{
var arr = val as Array;
var arrClone = arr.Clone() as Array;
for (int index = 0; index < arr.Length; index++)
{
var a = arr.GetValue(index);
if (a.GetType().IsValueType || a is string) continue;
arrClone.SetValue(Activator.CreateInstance(a.GetType()).InjectFrom<CloneInjection>(a), index);
}
return arrClone;
}
if (sp.PropertyType.IsGenericType)
{
//handle IEnumerable<> also ICollection<> IList<> List<>
if (sp.PropertyType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
{
var genericType = tp.PropertyType.GetGenericArguments()[0];
var listType = typeof(List<>).MakeGenericType(genericType);
var list = Activator.CreateInstance(listType);
var addMethod = listType.GetMethod("Add");
foreach (var o in val as IEnumerable)
{
var listItem = genericType.IsValueType || genericType == typeof(string) ? o : Activator.CreateInstance(genericType).InjectFrom<CloneInjection>(o);
addMethod.Invoke(list, new[] { listItem });
}
return list;
}
//unhandled generic type, you could also return null or throw
return val;
}
return Activator.CreateInstance(tp.PropertyType)
.InjectFrom<CloneInjection>(val);
}
}
我这样使用:
var entityDto = new EntityDto().InjectFrom<CloneInjection>(sourceEntity);
希望对您有帮助!