Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 使用IMapperConfigurator实现自动将枚举映射到字节_C# 4.0_Automapper - Fatal编程技术网

C# 4.0 使用IMapperConfigurator实现自动将枚举映射到字节

C# 4.0 使用IMapperConfigurator实现自动将枚举映射到字节,c#-4.0,automapper,C# 4.0,Automapper,枚举定义为 public enum RowStatusEnum { Modified = 1, Removed = 2, Added = 3 } public class RowStatusEnumConvertor : IMapperConfigurator { public void Cofigure() { Mapper.CreateMap<RowStatusEnum, byte>(); Mapper.C

枚举定义为

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

public class RowStatusEnumConvertor : IMapperConfigurator
{
    public void Cofigure()
    {
        Mapper.CreateMap<RowStatusEnum, byte>();
        Mapper.CreateMap<byte, RowStatusEnum >();
    }
}
公共枚举行状态枚举
{
修改=1,
删除=2,
已添加=3
}
公共类RowStatusEnumConvertor:IMapperConfigurator
{
public void Cofigure()
{
CreateMap();
CreateMap();
}
}
我在RowStatusEnumConvertor类中使用Implementation IMapperConfigurator配置autoMapper, 但是不能使用此代码,也不能映射此类型,我认为我的配置不正确或不够, 请帮帮我


谢谢

我重现了你的问题。解决方案非常简单,不需要配置AutoMapper,而是将枚举的基类型设置为byte。像这样:

public enum RowStatusEnum : byte
{
   Modified = 1,
   Removed = 2,
   Added = 3,
}
要让它发挥作用:

byte x = 3;
RowStatusEnum rowStatus = Mapper.Map<RowStatusEnum>(x); 
//The result will be: Added
字节x=3;
RowStatusEnum rowStatus=Mapper.Map(x);
//结果将是:添加

像这样的东西对你有用吗

Classes.cs

namespace StackOverflow.RowStatus
{
公共枚举行状态枚举
{
修改=1,
删除=2,
已添加=3
}
}
AutoMapperConfigurator.cs

namespace StackOverflow.RowStatus
{
使用制度;
使用System.Linq;
使用自动制版机;
公共类MyProfile:Profile
{
受保护的覆盖无效配置()
{
Mapper.CreateMap().ConvertUsing(
x=>Enum.GetValues(typeof(RowStatusEnum))
.Cast().First(y=>(字节)y==x));
Mapper.CreateMap().ConvertUsing(
x=>(字节)x);
}
}
}
MappingTests.cs

namespace StackOverflow.RowStatus
{
使用自动制版机;
使用NUnit.Framework;
[测试夹具]
公共类映射测试
{
[测试]
public void AutoMapper_配置_IsValid()
{
初始化(m=>m.AddProfile());
assertConfigurationsValid();
}
[测试用例(1,结果=RowStatusEnum.Modified)]
[TestCase(2,结果=RowStatusEnum.Removed)]
[TestCase(3,结果=RowStatusEnum.Added)]
public RowStatusEnum AutoMapper_ConvertFromByte_有效(
字节(行状态枚举)
{
初始化(m=>m.AddProfile());
assertConfigurationsValid();
返回Mapper.Map(rowStatusEnum);
}
[测试用例(RowStatusEnum.Modified,结果=1)]
[测试用例(RowStatusEnum.Removed,结果=2)]
[测试用例(RowStatusEnum.Added,结果=3)]
公共字节自动映射\u ConvertFromEnum\u有效(
RowStatusEnum(RowStatusEnum)
{
初始化(m=>m.AddProfile());
assertConfigurationsValid();
返回Mapper.Map(rowStatusEnum);
}
}
}

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

行状态枚举

namespace StackOverflow.RowStatus
{
公共枚举行状态枚举
{
修改=1,
删除=2,
已添加=3
}
}
AutoMapperConfigurator.cs

namespace StackOverflow.RowStatus
{
使用制度;
使用System.Linq;
使用自动制版机;
公共类MyProfile:Profile
{
公共MyProfile()
{
CreateMap().ConvertUsing(
x=>Enum.GetValues(typeof(RowStatusEnum))
.Cast().First(y=>(字节)y==x));
CreateMap().ConvertUsing(
x=>(字节)x);
}
}
}
映射测试

namespace StackOverflow.RowStatus
{
使用自动制版机;
使用NUnit.Framework;
[测试夹具]
公共类映射测试
{
[测试]
public void AutoMapper_配置_IsValid()
{
var config=new-MapperConfiguration(cfg=>cfg.AddProfile());
config.assertconfigurationsvalid();
}
[TestCase(1,ExpectedResult=RowStatusEnum.Modified)]
[TestCase(2,ExpectedResult=RowStatusEnum.Removed)]
[TestCase(3,ExpectedResult=RowStatusEnum.Added)]
public RowStatusEnum AutoMapper_ConvertFromByte_有效(
字节(行状态枚举)
{
var config=new-MapperConfiguration(cfg=>cfg.AddProfile());
var mapper=config.CreateMapper();
返回mapper.Map(rowStatusEnum);
}
[测试用例(RowStatusEnum.Modified,ExpectedResult=1)]
[测试用例(RowStatusEnum.Removed,ExpectedResult=2)]
[测试用例(RowStatusEnum.Added,ExpectedResult=3)]
公共字节自动映射\u ConvertFromEnum\u有效(
RowStatusEnum(RowStatusEnum)
{
var config=new-MapperConfiguration(cfg=>cfg.AddProfile());
var mapper=config.CreateMapper();
返回mapper.Map(rowStatusEnum);
}
}
}

我希望在IMapperConfigurator中配置此转换,并在项目的所有位置使用,而无需重新配置。以下几行是关键点:)Mapper.Initialize(m=>m.AddProfile());assertConfigurationsValid();感谢MightymukeShort版本似乎也能工作:
Mapper.CreateMap(RowStatusEnum)x)此答案已过时。