public enum CustomEnum { [System.ComponentModel.Description("I am Alpha")] Alpha, [System.ComponentModel.Description("Beta Friendly Label")] Beta } public static class EnumHelper { public static string GetEnumDescription(Enum value) { System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString()); System.ComponentModel.DescriptionAttribute[] attributes = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes( typeof(System.ComponentModel.DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } }
Usage:
MessageBox.Show(EnumHelper.GetEnumDescription(CustomEnum.Alpha));
No comments:
Post a Comment