本文介绍了AutoMapper使用实现IMapperConfigurator枚举到字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举定义

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

public class RowStatusEnumConvertor : IMapperConfigurator
{
    public void Cofigure()
    {
        Mapper.CreateMap<RowStatusEnum, byte>();
        Mapper.CreateMap<byte, RowStatusEnum >();
    }
}

我在RowStatusEnumConvertor类中配置了实现IMapperConfigurator的autoMapper,
但不工作这个代码,而不是映射这种类型,我想我的配置不正确或不够,
请帮助我

I config autoMapper with Implemention IMapperConfigurator in RowStatusEnumConvertor class, but not work this code and not map this type, i think my config not correct or not enough, please help me

感谢

推荐答案

我已转载您的问题。解决方案很简单,不要配置AutoMapper并将枚举的基本类型设置为字节。像这样:

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,
}
byte x = 3;
RowStatusEnum rowStatus = Mapper.Map<RowStatusEnum>(x); 
//The result will be: Added

这篇关于AutoMapper使用实现IMapperConfigurator枚举到字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:27