Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动映射子项_C#_Automapper - Fatal编程技术网

C# 自动映射子项

C# 自动映射子项,c#,automapper,C#,Automapper,我正在尝试使用Automapper从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方也这样做过,没有遇到麻烦。但在这种情况下,新创建的对象也需要映射其自身的属性 我在下面添加了一个通用版本 config.CreateMap<BusinessObject, WebObject>() .ForMember(d => d.Component, opts => opts.ResolveUsing(b => {

我正在尝试使用Automapper从前端对象层次结构映射到后端对象层次结构。这需要从源对象中的多个源动态创建子组件。我在其他地方也这样做过,没有遇到麻烦。但在这种情况下,新创建的对象也需要映射其自身的属性

我在下面添加了一个通用版本

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)”吗?