Obtains the name value of the enumerated Display through reflection. displayname
/// <Summary> // political outlook /// </summary> public enum EumPoliticSstatus {[Display (Name = "Party member")] PartyMember = 1, [Display (Name = "")] Member = 2, [Display (Name = "")] Masses = 3, [Display (Name = "")] democraticParty = 4}
First define Enumeration
public static string GetEnumDesc(Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false); if (attrs != null && attrs.Length > 0) return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).Name; } return en.ToString(); }
The above method obtains the name value in display through reflection based on the incoming enumerated value.
var name =GetEnumDesc(EumPoliticSstatus.PartyMember),