我正在尝试建立一种从一种类型到另一种类型的映射方法,因为它们应该具有相同的结构。
Related Question
.
为了方便使用fiddly位,我使用codeplex中的automapper,它具有以下功能:
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;
}
}
}
}
在单步执行代码时似乎工作正常,但是调用my map函数会出现以下错误:
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