zoukankan      html  css  js  c++  java
  • 反射相关函数获取枚举描述函数

    public static string GetEnumDescription(Enum enumValue)
                {
                    string value = enumValue.ToString();
                    FieldInfo field = enumValue.GetType().GetField(value);
                    object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
                    if (objs.Length == 0)    //当描述属性没有时,直接返回名称
                        return value;
                    DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                    return descriptionAttribute.Description;
                }
      PropertyInfo[] peroperties = typeof(companyEnum).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                foreach (PropertyInfo property in peroperties)
                {
                    object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (objs.Length > 0)
                    {
                        ControlFiledData cfd = new ControlFiledData();
                        cfd.comname = "txt" + property.Name;
                        cfd.labtext = ((DescriptionAttribute)objs[0]).Description;
                        // Console.WriteLine("{0}: {1}", property.Name, ((DescriptionAttribute)objs[0]).Description);
                        ListData.Add(cfd);
                    }
                }
  • 相关阅读:
    HangFire快速入门
    HangFire概述
    Lodop错误汇总
    微信支付过程遇到的问题
    动态规划算法
    几句话~
    Welcom To My Blog
    【技巧】关于素数
    安徽省小学组省赛2014年第一题 木板面积(C++)
    洛谷 P1425 小鱼的游泳时间
  • 原文地址:https://www.cnblogs.com/wangchuang/p/10079759.html
Copyright © 2011-2022 走看看