我有班级课程:
{
"operations": [
{
"method": "GET",
"path": "/reports/campaign_id/email-activity",
"operation_id" : "123"
}
]
}
如何创建像
这样的映射class A
{
public string str1 { get; set; }
public string str2 { get; set; }
public string str3 { get; set; }
}
class B
{
public string bstr1 { get; set; }
public string bstr2 { get; set; }
}
class C
{
public string cstr1 { get; set; }
public IEnumerable<B> barr1 { get; set; }
}
简单的说明如何将对象映射到目标中数组的第一项。
答案 0 :(得分:1)
您可以执行以下操作:
Mapper.CreateMap<A, B>()
.ForMember(dest => dest.bstr1, exp => exp.MapFrom(src => src.str2))
.ForMember(dest => dest.bstr2, exp => exp.MapFrom(src => src.str3));
Mapper.CreateMap<A, C>()
.ForMember(dest => dest.cstr1, exp => exp.MapFrom(scc => scc.str1))
.ForMember(dest => dest.barr1, exp => exp.ResolveUsing<AToBCollectionResolver>());
AToBCollectionResolver
:
class AToBCollectionResolver : ValueResolver<A, IEnumerable<B>>
{
protected override IEnumerable<B> ResolveCore(A source)
{
return new[] { Mapper.Map<B>(source)};
}
}