AutoMapper Enum to byte with implemention IMapperConfigurator

前端 未结 3 1482
一生所求
一生所求 2021-02-07 11:25

Enum definition is

public enum RowStatusEnum
{
    Modified = 1,
    Removed = 2,
    Added = 3
}

public class RowStatusEnumConvertor : IMapperConfigurator
{
           


        
3条回答
  •  春和景丽
    2021-02-07 12:12

    I have reproduced your problem. The solution is pretty simple, don't configure AutoMapper and set the base type of the enum to byte. Like this:

    public enum RowStatusEnum : byte
    {
       Modified = 1,
       Removed = 2,
       Added = 3,
    }
    

    To let it work:

    byte x = 3;
    RowStatusEnum rowStatus = Mapper.Map(x); 
    //The result will be: Added
    

提交回复
热议问题