2

I'm trying to build a way of mapping from one type to another, knwoing they will (should) have the same structure. Related Question.

For ease of the fiddly bits, I'm using AutoMapper from Codeplex, with the following function:

private static List<Type> seenTypes = new List<Type>();

private static void MapDataObjects(Type a, Type b)
{
    AutoMapper.Mapper.CreateMap(a, b);

    PropertyInfo[] aProps = a.GetProperties();
    PropertyInfo[] bProps = b.GetProperties();
    foreach (PropertyInfo aProp in aProps)
    {
        if (aProp.PropertyType.Namespace.StartsWith("System") 
             || seenTypes.Contains(aProp.PropertyType))
            continue;

        foreach (PropertyInfo bProp in bProps)
        {
            if (aProp.Name == bProp.Name)
            {
                MapDataObjects(aProp.PropertyType, bProp.PropertyType);
                seenTypes.Add(aProp.PropertyType);
                break;
            }
        }
    }
 }

Which appears to work fine when stepping through the code, however calling my Map function gives the following error:

AutoMapper.AutoMapperMappingException: 
     Trying to map TDXDataTypes.ClientActivity[] to ClientActivity[].
Using mapping configuration for TDXDataTypes.ClientActivity[] to ClientActivity[]
Destination property: Activities
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. 
    ---> System.ArgumentException: 
         Type 'ClientActivity[]' does not have a default constructor
1

1 Answer 1

1

Found the solution (should have tried harder before asking the question):

 if (aProp.Name == bProp.Name)
    {
        if (aProp.PropertyType.IsArray)
        {
            MapDataObjects(aProp.PropertyType.GetElementType(), bProp.PropertyType.GetElementType());
            seenTypes.Add(aProp.PropertyType.GetElementType());
            break;
        }
        else
        {
            MapDataObjects(aProp.PropertyType, bProp.PropertyType);
            seenTypes.Add(aProp.PropertyType);
            break;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, you only need to map element types for collections, not the collections themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.