/// <summary>/// 政治面貌/// </summary>public enum EumPoliticSstatus { [Display(Name = "党员")] PartyMember = 1, [Display(Name = "团员")] Member = 2, [Display(Name = "群众")] Masses = 3, [Display(Name = "民主党派")] DemocraticParty = 4}
先定義枚舉
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(); }
上面這個方法根據傳入的枚舉值透過反射來獲得display中name的值
var name =GetEnumDesc(EumPoliticSstatus.PartyMember),
#
以上是詳解如何透過反射來獲得枚舉的Display中name的值實例?的詳細內容。更多資訊請關注PHP中文網其他相關文章!