using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; /// /// Contains extension methods. /// public static class EnumExtensions { /// /// Gets the Enum description attribute value. /// /// The enum value. /// public static String ToDescription(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } public static T GetAttribute(this Enum value) where T : Attribute { FieldInfo fi = value.GetType().GetField(value.ToString()); return fi.GetCustomAttribute(); } /// /// Gets the enum integer value. /// /// The value. /// public static int ToInt32(this Enum value) { return (int)((object)value); } /// /// Gets all the flags from a bitwise enumeration. /// /// /// The enum. /// public static IEnumerable GetFlags(this Enum input) { foreach (Enum value in Enum.GetValues(input.GetType())) if (input.HasFlag(value)) yield return (T)(object)value; } }