FileHelpers icon indicating copy to clipboard operation
FileHelpers copied to clipboard

Extend EnumConverter to use EnumMemberAttribute

Open jkonecki opened this issue 3 years ago • 1 comments

EnumConverter is internally using Enum.Parse method that assumes that the parsed value matches the name of the enum value.

System.Runtime.Serialization has an EnumMemberAttribute (https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.enummemberattribute) that can be used to decorate each field and specify a different value:

public enum Position
{
    [EnumMember(Value = "Emp")]
    Employee,
    [EnumMember(Value = "Mgr")]
    Manager,
    [EnumMember(Value = "Ctr")]
    Contractor
}

This allows for easy definition of mapings between 'proper' names of enum options and the actual string values that will be serialized.

I would like to extend EnumConverter to utilize the EnumMemberAttribute during conversion.

Please let me know if you would be interested in my PR for this change.

jkonecki avatar Feb 18 '22 22:02 jkonecki

Bump

jonnyruffles avatar Jun 20 '23 14:06 jonnyruffles