/// <summary> /// Returns a string description for the enum /// </summary> /// <typeparam name="TEnumType">The enum value.</typeparam> /// <returns></returns> public static string DiscriptionFor(Enum value) { string display = Enum.GetName(value.GetType(), value); FieldInfo field = value.GetType().GetField(value.ToString()); if (field != null) { foreach (Attribute currAttr in field.GetCustomAttributes(true)) { DisplayAsAttribute valueAttribute = currAttr as DisplayAsAttribute; if (valueAttribute != null) display = valueAttribute.Name; } } return display; }
/// <summary> /// Returns a dictionary containing all values of an enumeration type /// </summary> /// <typeparam name="TEnumType">The type of the enum type.</typeparam> /// <returns></returns> public static Dictionary<int, string> DictionaryOf<TEnumType>() { Dictionary<int, string> dictionary = new Dictionary<int, string>(); Type enumType = typeof(TEnumType); if (enumType.IsEnum) { foreach(FieldInfo field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public)) { int value = (int)field.GetValue(null); string display = Enum.GetName(enumType, value); foreach(Attribute currAttr in field.GetCustomAttributes(true)) { DisplayAsAttribute valueAttribute = currAttr as DisplayAsAttribute; if (valueAttribute != null) display = valueAttribute.Name; } dictionary.Add(value, display); } } return dictionary; }