【问题标题】:omu.valueinjecter deep clone unlike typesomu.valueinjecter 深度克隆不同类型
【发布时间】:2012-01-05 04:47:32
【问题描述】:

我认为我缺少 valueinjecter 和/或 AutoMapper 的简单概念,但是如何将父 dto.Entity 深度克隆到 biz.Entity 并包括所有子级?

例如,biz.person.InjectFrom(dto.person)。我希望 dto.person.AddressList 集合复制到 biz.person.AddressList 集合,即使 dto.Addressbiz.Address 类型不同,但具有相同的属性名称。

我的想法是,如果父属性名称拼写相同,例如AddressList,那么如果 2 个底层对象属于不同类型,则无关紧要。它仍然会复制同名的简单类型,如 int、string 等。

谢谢

【问题讨论】:

  • 您是否查看过 ValueInjecter 的 codeplex 页面中的 Deep Cloning 页面? valueinjecter.codeplex.com/…
  • 嘿查克。是的,我做到了。它没有深度克隆我的 [nhibernate] 子实体
  • 如果您发布一些代码会很好,并且关于不同类型,默认 InjectFrom() 从相同名称和相同类型注入,因此它不会影响具有不同类型的成员(如果您正在从一种类型转换为另一种不再克隆的类型,这就是 DeepClone 不适合您的原因)

标签: wcf valueinjecter


【解决方案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);

希望对你有帮助!

【讨论】:

    【解决方案2】:

    当对象中的数组/列表具有相同的名称但类型不同时,我遇到了同样的问题(即 ORMAnimals[] 类型的 Animals 属性映射到 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);
        }
    }
    

    【讨论】:

    • addMethod.Invoke(list, new[] { e }); // 在 4.0 中你可以使用 dynamic 并且只做 list.Add(e);有人做过吗?我尝试用“动态列表”替换“var list”,然后执行 list.Add(e),它可以编译但会引发运行时异常。
    • @Gloopy - 试过了,但我一直收到堆栈溢出:(
    • @Ryan 是否有可能您的一个对象引用回自身或另一个父对象在遍历子对象时导致无限循环?
    • @Gloopy - 是的,情况就是这样,我无法弄清楚。我正在使用没有代理或延迟加载的实体框架 5。有没有办法在有循环引用的任何时候定义一个停止点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多