How to clone collection properties?

Feb 26, 2013 at 8:56 PM
I switched to VI from Automapper because I'm in SL5.

After weeks of development on a project I just hit a roadblock cloning objects that have collection properties. Getting nervous!

I'm converting simple DTOs to Presentation objects. They're simple and have the same property names. I can convert a DTO into a PE with a stock InjectFrom() call.

But when I get to a DTO with a property of child DTOs that property is not cloned. I found an injection helper that I thought would solve the problem, but it doesn't work.

Object of type 'System.Collections.Generic.List1[Dto.Pathology]' cannot be converted to type 'System.Collections.Generic.List1[Pe.Pathology]'.

I was hoping for a drop-in solution that I don't have to become an expert in understanding the inner workings. I suspect it's because the injector's trying to assign the source to the target directly, and of course they're not the same class. Isn't there a way to recursively call the stock injector for each child list it finds? The stock one handles the basic DTO->PE conversions just fine.

I'd be so thrilled if someone has a solution for this!

Thank you!

Here's the code I found:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Omu.ValueInjecter.Silverlight;

namespace IntrinsiQ.DataOps.MappingTool.Client.Helpers
{
    public class CloneInjection : ConventionInjection
    {
        public override bool Match(ConventionInfo c)
        {
            return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null;
        }

        public 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))
                return c.SourceProp.Value;

            //handle arrays
            if (c.SourceProp.Type.IsArray)
            {
                var arr = c.SourceProp.Value as Array;
                var clone = arr.Clone() 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(a.GetType()).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.SourceProp.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.SourceProp.Type)
                .InjectFrom<CloneInjection>(c.SourceProp.Value);
        }
    }
}
Feb 26, 2013 at 10:32 PM
as you may noticed the generic type is different, try updating this line

var t = c.SourceProp.Type.GetGenericArguments()[0];

to:

var t = c.TargetProp.Type.GetGenericArguments()[0];

you'll have to try and understand how the clone injection works,

it is already recursive, note the .InjectFrom<CloneInjection> calls inside it
Feb 27, 2013 at 12:48 PM
That works perfectly. Thank you!
Jun 12, 2013 at 6:37 PM
Activator.CreateInstance only works for types with a parameterless constructor. I'm trying to use FormatterServices.GetUninitializedObject instead, but I can't be able to call the extension method InjectFrom from the instance obtained.