AutoMapper Enum to byte with implemention IMapperConfigurator

两盒软妹~` 提交于 2019-12-13 11:36:39

问题


Enum definition is

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

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

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

thanks


回答1:


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<RowStatusEnum>(x); 
//The result will be: Added



回答2:


Will something like this work for you?

Classes.cs

namespace StackOverflow.RowStatus
{
    public enum RowStatusEnum
    {
        Modified = 1,
        Removed = 2,
        Added = 3
    }
}

AutoMapperConfigurator.cs

namespace StackOverflow.RowStatus
{
    using System;
    using System.Linq;

    using AutoMapper;

    public class MyProfile : Profile
    {
        protected override void Configure()
        {
            Mapper.CreateMap<byte, RowStatusEnum>().ConvertUsing(
                x => Enum.GetValues(typeof(RowStatusEnum))
                         .Cast<RowStatusEnum>().First(y => (byte)y == x));
            Mapper.CreateMap<RowStatusEnum, byte>().ConvertUsing(
                x => (byte)x);
        }
    }
}

MappingTests.cs

namespace StackOverflow.RowStatus
{
    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
        }

        [TestCase(1, Result = RowStatusEnum.Modified)]
        [TestCase(2, Result = RowStatusEnum.Removed)]
        [TestCase(3, Result = RowStatusEnum.Added)]
        public RowStatusEnum AutoMapper_ConvertFromByte_IsValid(
                                                   byte rowStatusEnum)
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
            return Mapper.Map<byte, RowStatusEnum>(rowStatusEnum);
        }

        [TestCase(RowStatusEnum.Modified, Result = 1)]
        [TestCase(RowStatusEnum.Removed, Result = 2)]
        [TestCase(RowStatusEnum.Added, Result = 3)]
        public byte AutoMapper_ConvertFromEnum_IsValid(
                                                   RowStatusEnum rowStatusEnum)
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
            return Mapper.Map<RowStatusEnum, byte>(rowStatusEnum);
        }
    }
}


来源:https://stackoverflow.com/questions/14108080/automapper-enum-to-byte-with-implemention-imapperconfigurator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!