easyui 开发中的通用类库

1.Enum转DataTable 

        public enum LogCatalogEnum
        {
            [DescriptionAttribute("fdsa")]
            全部 = 0,
            系统事件 = 1,
            用户事件 = 2
        }

转换成DataTable:

代码:

View Code
 1    /// <summary>
 2         /// 枚举转成DataTable,有三列:
 3         /// Name:System.String 
 4         /// Value:long
 5         /// Desc:System.String
 6         /// </summary>
 7         /// <param name="enumType">枚举类型,如果不是会抛出InvalidOperationException异常</param>
 8         /// <returns>枚举对应的DataTable</returns>
 9         public static DataTable Enum2DataTable(Type enumType)
10         {
11             if (enumType.IsEnum != true)
12                 throw new InvalidOperationException();
13 
14 
15             //建立DataTable的列信息
16             DataTable dt = new DataTable();
17             dt.Columns.Add("Name", typeof(System.String));
18             dt.Columns.Add("Value", typeof(long));
19             dt.Columns.Add("Desc", typeof(System.String));
20 
21             //获得特性Description的类型信息
22             Type typeDescription = typeof(DescriptionAttribute);
23             System.Reflection.FieldInfo[] fields = enumType.GetFields();
24 
25             //检索所有字段
26             foreach (FieldInfo field in fields)
27             {
28                 if (field.FieldType.IsEnum == true)
29                 {
30                     DataRow dr = dt.NewRow();
31                     // 通过字段的名字得到枚举的值
32                     // 注意枚举的值需要时long类型
33                     dr["Name"] = field.Name;
34                     dr["Value"] = (long)(int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
35 
36                     object[] arr = field.GetCustomAttributes(typeDescription, true);
37                     if (arr.Length > 0)
38                     {
39                         DescriptionAttribute desc = (DescriptionAttribute)arr[0];
40                         dr["Desc"] = desc.Description;
41                     }
42                     else
43                     {
44                         dr["Desc"] = field.Name;
45                     }
46                     dt.Rows.Add(dr);
47                 }
48             }
49 
50             return dt;
51         }
原文地址:https://www.cnblogs.com/zhaobl/p/2892625.html