更新时间:2022-05-14 22:14:34
我知道它确实很旧,但是我确实找到了答案(至少对于最新版本的AutoMapper(在此答案时为8.0).为Person写一个自定义的 ITypeConverter
来完成对象的映射[].
I know this is really old but I did find the answer (at least for the latest version of AutoMapper (8.0 at time of this answer). You need to write a custom ITypeConverter
for Person that does the mapping of the object[].
在我的情况下,我有一个像这样的对象(删除了xml属性):
In my case i had an object like this (xml attributes removed):
namespace bob {
public class sally
{
public bob.MyEnum[] SomeVarName {get;set;}
public object[] SomeValueVar {get;set;}
}
}
namespace martin {
public class sally
{
public martin.MyEnum[] SomeVarName {get;set;}
public object[] SomeValueVar {get;set;}
}
}
自定义类型转换器如下所示:
The custom type converter looked like this:
public class LocationTypeResolver : ITypeConverter<bob.sally,martin.sally>
{
public martin.sally Convert(bob.sally source, martin.sally destination, ResolutionContext context)
{
var retVal = new martin.sally
{
SomeValueVar = new object[source.SomeVarName.Length],
SomeVarName = new martin.MyEnum[source.SomeVarName.Length]
};
for (int i = 0; i < source.Items.Length; i++)
{
retVal.SomeVarName[i] = (martin.MyEnum)Enum.Parse(typeof(martin.MyEnum), source.SomeVarName[i].ToString());
switch (source.ItemsElementName[i])
{
//map any custom types
default:
retVal.SomeValueVar[i] = source.SomeValueVar[i]
}
}
return retVal;
}
}