I'm having difficulties mapping from an Enum Description Attribute. I've been looking all over for a useful example with very little luck. I know there are some other examples out there but I'm still struggling with this particular scenario.
Here is my Enum:
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
}
Here is my Enum Helper 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();
}
}
My goal is to map source to destination via Enum Description Attribute
Here is what I have so far in my mapping configuration:
Mapper.CreateMap<Result, Incident>()
.ForMember(dest => dest.Status,
opts => opts.MapFrom(src => src.state));
Here is the abbreviated Result class:
public class Result
{
public string sys_id { get; set; }
public string state { get; set; }
}
Here is the abbreviated Incident class:
public class Incident
{
public string Id{ get; set; }
public string Status{ get; set; }
}
Note: the state property of the Result class is a string
For example:
My goal is to get
Incident.Status = "Resolved - Workaround"
From
Result.state = "1"
I've been struggle with the automapper syntax to use with my EnumHelper class
If anybody can help me out on this, it would be greatly appreciated :)
Two ways to do this:
Inline with
ResolveUsing
:With a custom
ValueResolver
:Usage:
I'd recommend #2 since it's much cleaner.