Mapping one source class to multiple derived classes with automapper

后端 未结 3 1378
忘掉有多难
忘掉有多难 2021-02-03 22:46

Suppose i have a source class:

public class Source
{
    //Several properties that can be mapped to DerivedBase and its subclasses
}

And some d

3条回答
  •  礼貌的吻别
    2021-02-03 23:44

    NB! For those who are having issues with derived interfaces. AutoMapper does not support registering against derived interfaces. Only classes are handled.

    To make it work, you have to change your type reference for CreateMap to the class instead of interface.

    Example:

    interface Interface1 {}
    class Class1: Interface1 {}
    interface Interface2: Interface1 {}
    class Class2: Class1, Interface2 {}
    
    CreateMap().IncludeAllDerived();
    CreateMap();
    

    Any mapping against Interface2 will only use the first CreateMap. You will have to identify the second as

    CreateMap();
    

提交回复
热议问题