zoukankan      html  css  js  c++  java
  • 利用发射 下拉列表绑定枚举

      public static DataTable GetDataTable(Type enumType)
            {
               // NameValueCollection nvc = new NameValueCollection();
                Type typeDescription = typeof(DescriptionAttribute);
                System.Reflection.FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                DataTable table = new DataTable();
                table.Columns.Add("Name", Type.GetType("System.String"));
                table.Columns.Add("Value", Type.GetType("System.Int32"));

                foreach(FieldInfo field in fields)
                {
                    if(field.FieldType.IsEnum == true)
                    {
                        DataRow row = table.NewRow();

                        strValue = ((int)enumType.InvokeMember(field.Name,BindingFlags.GetField,null,null,   null)).ToString();

                        row[1] = strValue;
                       object[] arr = field.GetCustomAttributes(typeDescription,true);
                      if (arr.Length> 0)
                        {
                            DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                         
                            row[0] = aa.Description;
                        }
                        else
                        {
                            row[0] = field.Name;
                        }
                        //nvc.Add(strText,strValue);
                        table.Rows.Add(row);

                    }
                }
                return table;
             }

  • 相关阅读:
    luncence
    git与svn与github与码云的区别
    redis缓存在项目中的使用
    大宗风控体系“药不能停”:一线实战高手解密衍生品交易风险管控的三个层级!
    永恒的风控:大宗商品贸易融资背后的核心风险该如何规避?
    达信:深度解读COSO新版企业风险管理框架(ERM)
    DataOps Reading Notes
    SDN Reading Notes
    给某mooc站点准备的FE大纲
    《财富》杂志推荐的75本商业必读书?
  • 原文地址:https://www.cnblogs.com/youngerliu/p/2569573.html
Copyright © 2011-2022 走看看