Retrieve Member Value Text for Enum
The following method illustrates how to retrieve the EnumMember text value of a given Enum value.
public class Utilities
{
public static string? GetEnumMemberAttrValue(T enumValue) where T : struct, IConvertible
{
var enumType = typeof(T);
var memInfo = enumType.GetMember(enumValue.ToString() ?? string.Empty);
var attr = memInfo?.FirstOrDefault()?.GetCustomAttributes(false).OfType().FirstOrDefault();
if (attr == null)
{
return null;
}
return attr.Value;
}
}
Related
- Serialize Enum values with JSON structure - Illustrates how to set up an Enum property so that the enum value name is serialized
Last modified by Mohit @ 4/20/2025 1:47:41 PM