我是靠谱客的博主 虚拟小甜瓜,这篇文章主要介绍详解如何通过反射获得枚举的Display中name的值实例?,现在分享给大家,希望可以做个参考。

         /// <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的值实例?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是虚拟小甜瓜最近收集整理的关于详解如何通过反射获得枚举的Display中name的值实例?的全部内容,更多相关详解如何通过反射获得枚举内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部