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

发布于 2024-12-18 00:28:49 字数 371 浏览 0 评论 0原文

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

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

我的想法是,如果 Parent 属性名称拼写相同,例如 AddressList,那么两个底层对象是否具有不同类型也没关系。它仍然会复制同名的简单类型,如 int、string 等。

谢谢

I think I'm missing a simple concept with valueinjecter and/or AutoMapper, but how do you deep clone a parent dto.Entity to biz.Entity and include all children?

For example, biz.person.InjectFrom(dto.person). I want the dto.person.AddressList collection to copy down to biz.person.AddressList collection, even though dto.Address and biz.Address are unlike types, but have the same property names.

My thinking is that if the Parent property names are spelled the same, e.g. AddressList, then it wouldn't matter if the 2 underlying objects were of different types. It would still copy same-named simple types like int, string, etc.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-12-25 00:28:49

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

I was having the same issue when the arrays/lists in the objects have the same names but different types (ie a property named Animals of type ORMAnimals[] mapping to a property named Animals of type Animals[]).

With some minor tweaks to the sample code Chuck Norris has on the Deep Cloning page I got it working in my test code:

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);
    }
}
只有影子陪我不离不弃 2024-12-25 00:28:49

我遇到了这个问题,即使使用 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);

希望有帮助!

I had this issue, even using the CloneInjection wasn't working to copy the properties with same name and diferent types. So I changed a few things in the CloneInjection (I'm using ValueInjecter version 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);
    }
}

I used like this:

var entityDto = new EntityDto().InjectFrom<CloneInjection>(sourceEntity);

I hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文