English 中文(简体)
AutoMapper: 将具体子类的源属性映射到目标类的 EnumValue
原标题:AutoMapper: Map Source property of concrete subclass to an EnumValue in Destination class

I would like to define a mapping (or even a TypeConverter/Resolver) for the following classes: Destination:

   public class Destination
    {
        public DestinationEnum EnumProperty { get; set; }

        public Destination()
        {
            EnumProperty = DestinationEnum.undefined;
        }
    }

    public enum DestinationEnum
    {
        oneValue,
        anotherValue, 
        undefined
    }

来源:

    public enum SourceEnum
    { 
        fu,
        ba
    }

    public enum AnotherSourceEnum
    { 
        first,
        second, 
        third
    }

    public class Source
    {
        public SourceEnum SourceEnumVal { get; set; }
    }

    public class ConcreteSource : Source
    {
        public AnotherSourceEnum ConcreteSourceEnumVal { get; set; }
    }

What would the AutoMapper Mapping look like, if I want to specify a destination value of the DestinationEnum in the Destination class depending on the concrete Source Type? E.g.

  • if the mapper maps from class "Source" to "Destination", the Destination.EnumProperty should be set to "undefined" if Source.SourceEnumVal == fu
  • if the mapper maps from class "source" to "Destination", the Destination.EnumProperty should be set to "oneValue" if Source.SourceEnumVal == "ba"
  • if the mapper maps from class "ConcreteSource" to "Destination", the Destination.EnumProperty should be set to "oneValue" if ConcreteSource.ConcreteSourceEnumVal == "second"
  • if the the mapper maps from class "ConcreteSource" to "Destination", the Destination.EnumProperty should be set to "undefined" if ConcreteSource.ConcreteSourceEnumVal != "second"
最佳回答

你将创建两张地图:

Mapper.CreateMap<Source, Destination>();
Mapper.CreateMap<ConcreteSource, Destination>();

然后针对每个地图进行自定义解析器:

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.EnumProperty, opt => opt.ResolveUsing<FirstResolver>());

您的第二个ConcreteSource地图将使用不同的解析器:

Mapper.CreateMap<ConcreteSource, Destination>()
    .ForMember(dest => dest.EnumProperty, opt => opt.ResolveUsing<SecondResolver>());

AutoMapper选择根据来源和目的地类型进行测绘,因此,无论使用哪一种来源类型,都会选择正确的决心。

问题回答

暂无回答




相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...

热门标签