///
/// Returns the text of the [Description("text")] attribute on an enum. Or null if not found.
///
public static String? Description( this Enum element ) {
var type = element.GetType();
var memberInfo = type.GetMember( element.ToString() );
if ( !memberInfo.Any() ) {
return null;
}
var attributes = memberInfo[ 0 ].GetCustomAttributes( typeof( DescriptionAttribute ), false );
return attributes.Any() ? ( attributes[ 0 ] as DescriptionAttribute )?.Description : null;
}
Like this:
Like Loading...
Related
Published by Protiguous
C# Software Developer, SQL Server DBA, Father, and seeker of Truth.
View all posts by Protiguous