我有一个面向公众的界面,我试图将两个不同的枚举映射到彼此.我试着使用以下代码:
Mapper.CreateMap<Contract_1_1_0.ValidationResultType, Common.ValidationResultType>();
Run Code Online (Sandbox Code Playgroud)
当那不起作用时,我试过:
Mapper.CreateMap<Contract_1_1_0.ValidationResultType, Common.ValidationResultType>().ConvertUsing(x => (Common.ValidationResultType)((int)x));
Run Code Online (Sandbox Code Playgroud)
但这似乎也不起作用.反正有没有让automapper来处理这种情况?
Tho*_*lin 62
或者编写自定义转换器,只需使用ConvertUsing()
Mapper.CreateMap<EnumSrc, EnumDst>().ConvertUsing(value =>
{
switch(value)
{
case EnumSrc.Option1:
return EnumDst.Choice1;
case EnumSrc.Option2:
return EnumDst.Choice2;
case EnumSrc.Option3:
return EnumDst.Choice3;
default:
return EnumDst.None;
}
});
Run Code Online (Sandbox Code Playgroud)
Jim*_*ard 43
您不需要为枚举类型执行CreateMap.只要名称和/或值在枚举类型之间匹配,就可以去除CreateMap调用并且它应该可以工作.
Pio*_*tek 11
我的Automapper以这种方式工作:
如果我创建了一个地图: Automapper将按值匹配枚举,即使名称完全匹配.
如果我不创建地图: Automapper将按名称匹配枚举.
这是在两个具有不同值的 Enum 类型之间进行转换的一种可能性,同时仍然使用 AutoMapper。就我而言,我需要使用 AutoMapper,因为 Enum 类型是由 AutoMapper 转换的其他实体的属性;要求对这些实体使用 AutoMapper。
第一步是设置 Mapper 配置,如下所示:
Mapper.CreateMap<EnumSrc, EnumDst>()
.ConstructUsing(EnumConversion.FromSrcToDst);
Run Code Online (Sandbox Code Playgroud)
调用.ConstructUsing(...)允许我们传入我们自己的方法来进行转换。转换方法非常简单:
public class EnumConversion
{
internal static EnumDst FromSrcToDst(ResolutionContext arg)
{
EnumSrc value = (EnumSrc)arg.SourceValue;
switch(value)
{
case EnumSrc.Option1:
return EnumDst.Choice1;
case EnumSrc.Option2:
return EnumDst.Choice2;
case EnumSrc.Option3:
return EnumDst.Choice3;
default:
return EnumDst.None;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们只需switch通过源 Enum 的值并任意返回适当的目标 Enum 值。AutoMapper 会处理剩下的事情。
这里的其他答案对我不起作用。
您需要创建一个实现的类:
ITypeConvertor<SourceType ,DestinationType>
Run Code Online (Sandbox Code Playgroud)
所以作为一个例子
Mapper.CreateMap<EnumType1.VatLevel, EnumType2.VatRateLevel>()
.ConvertUsing(new VatLevelConvertor());
Run Code Online (Sandbox Code Playgroud)
和班级:
internal class VatLevelConvertor : ITypeConverter<EnumType1.VatLevel, EnumType2.VatRateLevel>
{
public EnumType2.VatRateLevel Convert(ResolutionContext context)
{
EnumType1.VatLevel value = (EnumType1.VatLevel)context.SourceValue;
switch (value)
{
case EnumType1.VatLevel.Standard:
return EnumType2.VatRateLevel.Normal;
case EnumType1.VatLevel.Reduced:
return EnumType2.VatRateLevel.Lower;
case EnumType1.VatLevel.SuperReduced:
return EnumType2.VatRateLevel.Other;
default:
return EnumType2.VatRateLevel.Other;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现对我有用的最简单方法如下:
我的Enum嵌套在另一个类中,因此我使用ForMember方法和MapFrom如下:
Mapper.CreateMap<ProblematicCustomer, ProblematicCustomerViewModel>()
.ForMember(m=> m.ProblemType, opt=> opt.MapFrom(x=> (ProblemTypeViewModel)(int)x.ProblemType))
.ForMember(m=> m.JudgmentType, opt=> opt.MapFrom(x=> (JudgmentTypeViewModel)(int)x.JudgmentType));
Run Code Online (Sandbox Code Playgroud)
ProblemType和JudgmentType是枚举。它们的相关视图模型是ProblemTypeViewModel和JudgmentTypeViewModel,其成员与相关模型相同。
虽然我没有测试,但是我认为下面的代码应该对您有用:
Mapper.CreateMap<Contract_1_1_0.ValidationResultType, Common.ValidationResultType>()
.ForMember(m=> m, opt => opt.MapFrom(x=> (Common.ValidationResultType)(int)x);
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。
我知道这个问题很老了,但是如果像我这样的人路过这里......
从AutoMapper 文档中,现在有一个AutoMapper.Extensions.EnumMapping Nuget 包提供了一种简单的方法来实现这一点。
引用 AutoMapper 文档:
public enum Source
{
Default = 0,
First = 1,
Second = 2
}
public enum Destination
{
Default = 0,
Second = 2
}
internal class YourProfile : Profile
{
public YourProfile()
{
CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt
// optional: .MapByValue() or MapByName(), without configuration MapByValue is used
.MapValue(Source.First, Destination.Default)
)
.ReverseMap(); // to support Destination to Source mapping, including custom mappings of ConvertUsingEnumMapping
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34140 次 |
| 最近记录: |