枚举类型数据处理类

枚举类型在项目开发中,非常有用,可以避免代码中到处散落着0,1,2...这些不明白是代表什么意义的数字,必须在旁边加上注释。否则时间长了,谁也忘了这是什么。

下面是一个类,专门处理枚举类型数据的。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6 using System.ComponentModel;
 7 
 8 namespace Utils
 9 {
10     /// <summary>
11     /// 关于枚举的方法集
12     /// </summary>
13     public class EnumUtils
14     {
15         /// <summary>
16         /// 获取枚举数据的说明信息
17         /// </summary>
18         /// <param name="obj">枚举类型数据</param>
19         /// <returns></returns>
20         public static string GetDescription(Enum obj)
21         {
22             try
23             {
24                 string objName = obj.ToString();
25                 Type t = obj.GetType();
26                 FieldInfo fi = t.GetField(objName);
27                 DescriptionAttribute[] arrDesc = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
28 
29                 return arrDesc[0].Description;
30             }
31             catch
32             {
33                 return "";
34             }
35         }
36 
37         /// <summary>
38         /// 获取枚举属性值
39         /// </summary>
40         /// <param name="obj">枚举</param>
41         /// <returns></returns>
42         public static int GetValue(Enum obj)
43         {
44             return Convert.ToInt32(obj);
45         }
46 
47         /// <summary>
48         /// 根据值,获取对应枚举的说明信息
49         /// </summary>
50         /// <typeparam name="T">枚举类型</typeparam>
51         /// <param name="value"></param>
52         /// <returns></returns>
53         public static string GetDescription<T>(int value)
54         {
55             try
56             {
57                 T enumObj = GetEnum<T>(value);
58 
59                 string objName = enumObj.ToString();
60                 Type t = enumObj.GetType();
61                 FieldInfo fi = t.GetField(objName);
62                 DescriptionAttribute[] arrDesc = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
63 
64                 return arrDesc[0].Description;
65             }
66             catch
67             {
68                 return "";
69             }
70         }
71 
72         /// <summary>
73         /// 根据值 获取 枚举类型数据
74         /// </summary>
75         /// <typeparam name="T">枚举类型</typeparam>
76         /// <param name="value"></param>
77         /// <returns></returns>
78         public static T GetEnum<T>(int value)
79         {
80             T enumObj = (T)Enum.Parse(typeof(T), value.ToString());
81 
82             return (T)enumObj;
83         }
84 
85         
86     }
87 }

 

1、循环获取枚举数据项

如下面的枚举类型

1 public enum city
2         {
3             [Description("北京")]
4             beijing=0,
5             [Description("上海")]
6             shanghai=2,
7             [Description("广州")]
8             guangzhou=3
9         }

循环获取:

1 foreach (city ct in Enum.GetValues(city))
2 {
3       Console.WriteLine("城市名:" + EnumUtils.GetDescription(ct) + ",值:" + EnumUtils.GetValue(ct) + "\n");
4 }

结果:

城市名:北京,值:0

城市名:上海,值:1

城市名:广州,值:2

 

2、按值获取枚举类型数据

EnumUtils.GetEnum<city>(0);

返回的是city.beijing

先写到这里,以后有新方法再加。

 

posted on 2013-08-24 09:31  shengchang  阅读(262)  评论(0)    收藏  举报

导航