C# 自动映射子项
我正在尝试使用Automapper从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方也这样做过,没有遇到麻烦。但在这种情况下,新创建的对象也需要映射其自身的属性 我在下面添加了一个通用版本C# 自动映射子项,c#,automapper,C#,Automapper,我正在尝试使用Automapper从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方也这样做过,没有遇到麻烦。但在这种情况下,新创建的对象也需要映射其自身的属性 我在下面添加了一个通用版本 config.CreateMap<BusinessObject, WebObject>() .ForMember(d => d.Component, opts => opts.ResolveUsing(b => {
config.CreateMap<BusinessObject, WebObject>()
.ForMember(d => d.Component, opts => opts.ResolveUsing(b =>
{
return new ComponentBusinessObject()
{
Date = b.Property1.Date,
Definition = b.Property2.Definition // This needs converting from (DefinitionWebObject to DefinitionBusinessObject)
};
}));
config.CreateMap()
.FormMember(d=>d.Component,opts=>opts.ResolveUsing(b=>
{
返回新组件BusinessObject()
{
日期=b.属性1.日期,
Definition=b.Property2.Definition//这需要从(DefinitionWebObject到DefinitionBusinessObject)转换
};
}));
有人知道在较低级别重新调用映射器的方法吗?(“定义”在上面的例子中。)基于GTG的评论: 如果在映射
BusinessObject
和WebObject
之前将DefinitionWebObject
和DefinitionBusinessObject
映射在一起,您应该能够在父映射内调用Mapper.map
config.CreateMap();//首先创建子映射。
config.CreateMap()
.FormMember(d=>d.Component,opts=>opts.ResolveUsing(b=>
{
返回新组件BusinessObject()
{
日期=b.属性1.日期,
Definition=Mapper.Map(b.Property2.Definition)
};
}));
您尝试过使用“Definition=Mapper.Map(b.Property2.Definition)”吗?