小编Eri*_*hök的帖子

使用automapper将一个源类映射到多个派生类

假设我有一个源类:

public class Source
{
    //Several properties that can be mapped to DerivedBase and its subclasses
}
Run Code Online (Sandbox Code Playgroud)

还有一些目的地类:

public class DestinationBase
{
     //Several properties
}

public class DestinationDerived1 : DestinationBase
{
     //Several properties
}

public class DestinationDerived2 : DestinationBase
{
     //Several properties
}
Run Code Online (Sandbox Code Playgroud)

然后我希望派生的目标类继承baseclass的automapper配置,因为我不想重复它,有没有办法实现这个?

Mapper.CreateMap<Source, DestinationBase>()
    .ForMember(...)
    // Many more specific configurations that should not have to be repeated for the derived classes
    .ForMember(...);

Mapper.CreateMap<Source, DestinationDerived1 >()
    .ForMember(...);
Mapper.CreateMap<Source, DestinationDerived2 >()
    .ForMember(...);
Run Code Online (Sandbox Code Playgroud)

当我像这样写它时根本不使用基本映射,包含似乎对我没有帮助.

编辑:这是我得到的:

public class Source
{
    public string …
Run Code Online (Sandbox Code Playgroud)

c# automapper automapper-2

21
推荐指数
2
解决办法
2万
查看次数

标签 统计

automapper ×1

automapper-2 ×1

c# ×1