omu.valueinjecter deep clone unlike types

后端 未结 2 631
心在旅途
心在旅途 2021-01-05 08:14

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

相关标签:
2条回答
  • 2021-01-05 08:51

    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!

    0 讨论(0)
  • 2021-01-05 09:03

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题