Home > Backend Development > C#.Net Tutorial > Detailed explanation of how to obtain the value instance of name in the enumerated Display through reflection?

Detailed explanation of how to obtain the value instance of name in the enumerated Display through reflection?

零下一度
Release: 2017-06-23 16:18:18
Original
4122 people have browsed it
         /// <summary>/// 政治面貌/// </summary>public enum EumPoliticSstatus
        {
            [Display(Name = "党员")]
            PartyMember = 1,
            [Display(Name = "团员")]
            Member = 2,
            [Display(Name = "群众")]
            Masses = 3,
            [Display(Name = "民主党派")]
            DemocraticParty = 4}
Copy after login

First define the 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();
        }
Copy after login

The above method obtains the value of the name in the display through reflection based on the incoming enumeration value

 var name =GetEnumDesc(EumPoliticSstatus.PartyMember),
Copy after login

The above is the detailed content of Detailed explanation of how to obtain the value instance of name in the enumerated Display through reflection?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template