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

  • 相关阅读:
    NXOpen 各种参数(字符)类型转换
    NXOpen 重命名装配组件
    NXOpen 选择过滤
    【ASP.NET Core】一个默认的网站
    将博客搬至CSDN
    VS2017 .net core web项目 添加引用 报错 vs2017添加引用时报错未能正确加载“ReferenceManagerPackage”包
    负载均衡时,在State_Server模式中的Session共享问题(不讨论负载均衡的问题)
    修改linux文件权限命令:chmod
    Linux命令学习
    开闭原则
  • 原文地址:https://www.cnblogs.com/youngerliu/p/2569573.html
Copyright © 2011-2022 走看看