Automapper 枚举描述属性
Automapper Enum Description Attribute
我在从枚举描述属性映射时遇到困难。我一直在寻找一个有用的例子,但运气很少。我知道还有其他一些例子,但我仍在为这种特殊情况而苦苦挣扎。
这是我的枚举:
public enum ResolveCodeEnum
{
[Description("Resolved - Workaround")]
ResolvedWorkaround = 1,
[Description("Resolved - Permanently")]
ResolvedPermanently = 2,
[Description("Resolved - Unknown")]
ResolvedUnkown = 3,
[Description("Cannot Reproduce")]
CannotReproduce = 4,
[Description("Invalid")]
Invalid = 5,
[Description("Cancelled")]
Cancelled = 6
}
这是我的枚举助手 class:
public class EnumHelper
{
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
我的目标是通过枚举描述属性将源映射到目标
这是我目前的映射配置:
Mapper.CreateMap<Result, Incident>()
.ForMember(dest => dest.Status,
opts => opts.MapFrom(src => src.state));
这是简化的结果 class:
public class Result
{
public string sys_id { get; set; }
public string state { get; set; }
}
这是事件的缩写 class:
public class Incident
{
public string Id{ get; set; }
public string Status{ get; set; }
}
注意:Resultclass的状态属性是一个字符串
例如:
我的目标是
Incident.Status = "Resolved - Workaround"
来自
Result.state = "1"
我一直在努力使用我的 EnumHelper 的自动映射器语法 class
如果有人能帮我解决这个问题,我将不胜感激:)
两种方法:
内联 ResolveUsing
:
Mapper.CreateMap<Result, Incident>()
.ForMember(
dest => dest.Status,
opt => opt.ResolveUsing(src =>
{
var value = (ResolveCodeEnum)Enum.Parse(
typeof(ResolveCodeEnum), src.state);
return EnumHelper.GetEnumDescription(value);
}));
自定义ValueResolver
:
public class EnumValueResolver : ValueResolver<Result, string>
{
protected override string ResolveCore(Result src)
{
var value = (ResolveCodeEnum)Enum.Parse(typeof(ResolveCodeEnum), src.state);
return EnumHelper.GetEnumDescription(value);
}
}
用法:
Mapper.CreateMap<Result, Incident>()
.ForMember(
dest => dest.Status,
opt => opt.ResolveUsing<EnumValueResolver>());
我推荐 #2,因为它更干净。
我在从枚举描述属性映射时遇到困难。我一直在寻找一个有用的例子,但运气很少。我知道还有其他一些例子,但我仍在为这种特殊情况而苦苦挣扎。
这是我的枚举:
public enum ResolveCodeEnum
{
[Description("Resolved - Workaround")]
ResolvedWorkaround = 1,
[Description("Resolved - Permanently")]
ResolvedPermanently = 2,
[Description("Resolved - Unknown")]
ResolvedUnkown = 3,
[Description("Cannot Reproduce")]
CannotReproduce = 4,
[Description("Invalid")]
Invalid = 5,
[Description("Cancelled")]
Cancelled = 6
}
这是我的枚举助手 class:
public class EnumHelper
{
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
}
我的目标是通过枚举描述属性将源映射到目标
这是我目前的映射配置:
Mapper.CreateMap<Result, Incident>()
.ForMember(dest => dest.Status,
opts => opts.MapFrom(src => src.state));
这是简化的结果 class:
public class Result
{
public string sys_id { get; set; }
public string state { get; set; }
}
这是事件的缩写 class:
public class Incident
{
public string Id{ get; set; }
public string Status{ get; set; }
}
注意:Resultclass的状态属性是一个字符串
例如:
我的目标是
Incident.Status = "Resolved - Workaround"
来自
Result.state = "1"
我一直在努力使用我的 EnumHelper 的自动映射器语法 class
如果有人能帮我解决这个问题,我将不胜感激:)
两种方法:
内联
ResolveUsing
:Mapper.CreateMap<Result, Incident>() .ForMember( dest => dest.Status, opt => opt.ResolveUsing(src => { var value = (ResolveCodeEnum)Enum.Parse( typeof(ResolveCodeEnum), src.state); return EnumHelper.GetEnumDescription(value); }));
自定义
ValueResolver
:public class EnumValueResolver : ValueResolver<Result, string> { protected override string ResolveCore(Result src) { var value = (ResolveCodeEnum)Enum.Parse(typeof(ResolveCodeEnum), src.state); return EnumHelper.GetEnumDescription(value); } }
用法:
Mapper.CreateMap<Result, Incident>() .ForMember( dest => dest.Status, opt => opt.ResolveUsing<EnumValueResolver>());
我推荐 #2,因为它更干净。