枚举定义
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类配置autoMapper用FPGA实现IMapperConfigurator,但不起作用此代码,不,我想我的配置不正确或没有足够的映射这种类型的,请帮帮我
谢谢
我已复制你的问题。 该解决方案是非常简单的,不配置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
请问这样的事情对你的工作?
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);
}
}
}