18

枚举定义是

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,但无法使用此代码且未映射此类型,我认为我的配置不正确或不够,请帮助我

谢谢

4

3 回答 3

35

像这样的东西对你有用吗?

类.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);
        }
    }
}

映射测试.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);
        }
    }
}
于 2013-01-04T01:50:09.283 回答
19

9Automapper 版本开始,初始化发生了变化,导致上述示例过时。我更新了上面的示例并保留了原始值以引用旧版本(NUnit 3.12.0):

行状态枚举

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
    {
        public MyProfile()
        {
            CreateMap<byte, RowStatusEnum>().ConvertUsing(
                x => Enum.GetValues(typeof(RowStatusEnum))
                    .Cast<RowStatusEnum>().First(y => (byte)y == x));
            CreateMap<RowStatusEnum, byte>().ConvertUsing(
                x => (byte)x);
        }
    }
}

映射测试

namespace StackOverflow.RowStatus
{
    using AutoMapper;

    using NUnit.Framework;

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            config.AssertConfigurationIsValid();
        }

        [TestCase(1, ExpectedResult = RowStatusEnum.Modified)]
        [TestCase(2, ExpectedResult = RowStatusEnum.Removed)]
        [TestCase(3, ExpectedResult = RowStatusEnum.Added)]
        public RowStatusEnum AutoMapper_ConvertFromByte_IsValid(
            byte rowStatusEnum)
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            var mapper = config.CreateMapper();
            return mapper.Map<byte, RowStatusEnum>(rowStatusEnum);
        }

        [TestCase(RowStatusEnum.Modified, ExpectedResult = 1)]
        [TestCase(RowStatusEnum.Removed, ExpectedResult = 2)]
        [TestCase(RowStatusEnum.Added, ExpectedResult = 3)]
        public byte AutoMapper_ConvertFromEnum_IsValid(
            RowStatusEnum rowStatusEnum)
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<MyProfile>());
            var mapper = config.CreateMapper();
            return mapper.Map<RowStatusEnum, byte>(rowStatusEnum);
        }
    }
}
于 2020-08-30T21:53:12.430 回答
12

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

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

让它工作:

byte x = 3;
RowStatusEnum rowStatus = Mapper.Map<RowStatusEnum>(x); 
//The result will be: Added
于 2013-01-01T22:46:58.430 回答