by
192 12
0
5
2,631
1
Top 1% !
Popular
Famous
Tagged
Specified
Popularity: 6573rd place
This snippet is Public
.NETFramework3.5
LanguageCSharp
LicenseMIT_X11
MethodNameGetEnumDescriptionArray
OutputTypestring[]

C#: Get array string attributes of Enum object

Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1070/C---Get-array-string-attributes-of-Enum-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static string[] GetEnumDescriptionArray<T>() { Array enumValue = System.Enum.GetValues(typeof(T)); string[] result = new string[enumValue.Length]; Int32 count = 0; foreach (var item in enumValue) { FieldInfo fi = item.GetType().GetField(item.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false ); if (attributes != null && attributes.Length > 0) { result[count] = attributes[0].Description; } else { //return new string[0]; return null; } count++; } return result; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus