[CanBeNull]
public static String? GetDescription<T>( [CanBeNull] this T e ) where T : IConvertible {
if ( e is not Enum ) {
return default( String? );
}
var type = e.GetType();
foreach ( Int32 val in Enum.GetValues( type ) ) {
if ( val != e.ToInt32( CultureInfo.InvariantCulture ) ) {
continue;
}
var ename = type.GetEnumName( val );
if ( ename is null ) {
continue;
}
var memInfo = type.GetMember( ename );
if ( memInfo[0].GetCustomAttributes( typeof( DescriptionAttribute ), false ).FirstOrDefault() is DescriptionAttribute descriptionAttribute ) {
return descriptionAttribute.Description;
}
}
return default( String? );
}
Like this:
Like Loading...
Related
Published by Protiguous
C# Software Developer, SQL Server DBA, Father, and seeker of Truth.
View all posts by Protiguous