AutoMapper Enum to byte with implemention IMapperConfigurator

前端 未结 3 704
温柔的废话
温柔的废话 2021-02-07 11:44

Enum definition is

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

public class RowStatusEnumConvertor : IMapperConfigurator
{
           


        
3条回答
  •  离开以前
    2021-02-07 12:27

    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
    

提交回复
热议问题