This project is read-only.
1

Closed

Deep cloning issue with cloneinjection. (works with 2.3.3)

description

Had error with deep cloning using clone injection. The older version 2.3.3 works.

Basically I had:

public class GrandFather
{
public string name;
public List<Father> Fathers;
}

public class Father
{
public string name;
public List<Child> Children;
}

public class Child
{
public string name;
}

And when I tried to map to an object with similar structure. I had error. I then used 2.3.3 and it worked.
Closed Sep 3, 2016 at 8:20 PM by o
fork https://github.com/omuleanu/ValueInjecter and create a unit test (failing) showing the problem

comments

MapleLeafFan wrote Sep 12, 2016 at 1:00 PM

I found the fix. There's some lines of code missing from 2.3.3

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 = sp.PropertyType.GetGenericArguments()[0];

                // Added by JT: Fix for deep cloning
                if (sp.PropertyType.IsValueType || sp.PropertyType == typeof(string))
                {
                    return val;
                }

                // Added by JT: Fix for deep cloning
                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;
        }

        //for simple object types create a new instance and apply the clone injection on it
        return Activator.CreateInstance(sp.PropertyType)
                        .InjectFrom<CloneInjection>(val);
    }