omu.valueinjecter深度克隆不同于类型

use*_*212 7 wcf valueinjecter

我想我错过了一个带有valueinjecter和/或AutoMapper的简单概念,但是你如何深深地将父dto.Entity克隆到biz.Entity并包含所有孩子呢?

例如,biz.person.InjectFrom(dto.person).我想dto.person.AddressList集合到biz.person.AddressList收集拷贝,即使dto.Addressbiz.Address有不同的类型,但具有相同的属性名称.

我的想法是,如果Parent属性名称拼写相同,例如AddressList,那么2个底层对象是否属于不同类型并不重要.它仍然会复制同名的简单类型,如int,string等.

谢谢

Glo*_*opy 8

当对象中的数组/列表具有相同的名称但具有不同的类型(即名为Animals的类型为ORMAnimals []的属性映射到名为Animals类型为Animals []的属性)时,我遇到了同样的问题.

对Chuck Norris在Deep Cloning 页面上的示例代码进行了一些小调整,我在测试代码中使用了它:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)