using System.Collections.Generic;
using System.ComponentModel;
public static void Main()
foreach (var kvp in typeof(Test).ToDictionary<Test>())
Console.WriteLine(kvp.ToString());
[Description("A test enum value for 'Foo'")]
[Description("A test enum value for 'Bar'")]
public static class EnumExtensions
public static Dictionary<TEnum, string> ToDictionary<TEnum>(this Type type)where TEnum : struct, IComparable, IFormattable, IConvertible
return Enum.GetValues(type).OfType<TEnum>().ToDictionary(value => value, value => value.Description());
public static string Description<TEnum>(this TEnum @enum)where TEnum : struct, IComparable, IFormattable, IConvertible
var @string = @enum.ToString();
var attribute = @enum.GetType().GetField(@string).GetCustomAttribute<DescriptionAttribute>(false);
return attribute != null ? attribute.Description : @string;