zoukankan      html  css  js  c++  java
  • 反射获取枚举的属性注释

     public enum AccountType
        {       
            [Description("支付宝充值")]
            ALIPAY = 1,
      
            [Description("银联充值")]
            UNIONPAY = 2

    }

    public static string GetEnumName<T>(int value) where T : new()
            {
                Type t = typeof(T);
                foreach (MemberInfo mInfo in t.GetMembers())
                {
                    if (mInfo.Name == t.GetEnumName(value))
                    {
                        foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                        {
                            if (attr.GetType() == typeof(DescriptionAttribute))
                            {
                                return ((DescriptionAttribute)attr).Description;
                            }
                        }
                    }
                }
                return "";
            }

  • 相关阅读:
    express如何使用cors插件、body-parser插件
    如何让xshell关闭后依然运行node项目
    nuxt命令和部署
    Python—函数的名称空间
    Python—生成器
    Python—闭包
    Python入门-字符串常用方法
    Python入门-函数
    Python入门-文件操作
    Python入门-三级菜单
  • 原文地址:https://www.cnblogs.com/happyday56/p/3456195.html
Copyright © 2011-2022 走看看