Skip to content

Instantly share code, notes, and snippets.

@leandromoh
Created March 22, 2022 14:53
Show Gist options
  • Save leandromoh/52523716cef3ddd60cbe5bf0dc8c5ee4 to your computer and use it in GitHub Desktop.
Save leandromoh/52523716cef3ddd60cbe5bf0dc8c5ee4 to your computer and use it in GitHub Desktop.
public static class EnumExtensions
{
public static bool IsObsolete(this Enum value)
{
var enumType = value.GetType();
var enumName = enumType.GetEnumName(value);
var fieldInfo = enumType.GetField(enumName);
return Attribute.IsDefined(fieldInfo, typeof(ObsoleteAttribute));
}
public static string? GetDescription(this Enum value)
{
var enumType = value.GetType();
var enumName = enumType.GetEnumName(value);
if (enumName is null)
return null;
return enumType
.GetField(enumName)
?.GetCustomAttributes(typeof(DescriptionAttribute), true)
?.OfType<DescriptionAttribute>()
?.FirstOrDefault()
?.Description;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment