Created
March 22, 2022 14:53
-
-
Save leandromoh/52523716cef3ddd60cbe5bf0dc8c5ee4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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