开发者

Using AutoMapper to dynamically map objects including arrays

开发者 https://www.devze.com 2022-12-27 04:56 出处:网络
I\'m trying to build a way of mapping from one type to another, knwoing they will (should) have the same structure. Related Question.

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 TDXDa开发者_开发问答taTypes.ClientActivity[] to ClientActivity[]
Destination property: Activities
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. 
    ---> System.ArgumentException: 
         Type 'ClientActivity[]' does not have a default constructor


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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消