using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Text; namespace GxPress.Common.Extensions { public static class EnumExtensions { /// returns the localized Name, if a [Display] attribute is applied to the enum member /// returns null if there is no attribute public static string GetDisplayName(this Enum value) => value.GetEnumMemberAttribute()?.GetName(); public static string GetValue(this Enum value) => Enum.GetName(value.GetType(), value); public static IEnumerable GetEnums(this Enum e) { return Enum.GetValues(e.GetType()).Cast(); } private static TAttribute GetEnumMemberAttribute(this Enum value) where TAttribute : Attribute => value.GetType().GetEnumMemberAttribute(value.ToString()); private static TAttribute GetEnumMemberAttribute(this Type enumType, string enumMemberName) where TAttribute : Attribute => enumType.GetMember(enumMemberName).Single().GetCustomAttribute(); } }