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

  • 相关阅读:
    算法导论--平摊分析之聚集分析
    编译器开发系列--Ocelot语言3.类型名称的消解
    编译器开发系列--Ocelot语言2.变量引用的消解
    编译器开发系列--Ocelot语言1.抽象语法树
    算法导论--散列表的数学分析(精解)链表法
    Linux2.6内核协议栈系列--TCP协议2.接收
    日常‘说说’(回归 原森雨)
    那些玩枪战我特别想听到的声音!
    友链!
    晚安背后的秘密
  • 原文地址:https://www.cnblogs.com/youngerliu/p/2569573.html
Copyright © 2011-2022 走看看