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;
             }

  • 相关阅读:
    [转]android刷新后R.java不见了
    adb常用指令
    [转]Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
    effective c/C++
    七种布局显示方式效果及实现
    修改Tabhost样式和字体大小的方法
    [转]android中SoundRecorder
    java中的IO整理
    在xp下面下载Android源代码
    linux网络 (二):无线网络操作
  • 原文地址:https://www.cnblogs.com/youngerliu/p/2569573.html
Copyright © 2011-2022 走看看