关于ASP.NET中使用枚举存储“是否”,“已审核,未审核”,“各种定死的类型”

以前写项目都是把一些状态直接写到一个下拉框,直接写死了,但是越来越发现这种方式在后期的就不好维护了,比方说,一个产品中有状态“已审核,未审核”,状态分别为“1,0”,

一开始可能这种状态少了还是比较好管理,但是在后期,如果客户需要添加其他状态,比方说“初始,已作废”,就得在各种地方进行修改,关键可能还会遗漏某些地方,所以这里我写的文章里面主要介绍了枚举的使用,好处就是方便管理。

啥都不说,贴点代码

 1 public class AppEnum
 2     {
 3         public AppEnum()
 4         {
 5         }
 6         
 7         #region 工具函数
 8 
 9         public static SortedList GetStatus(System.Type t)
10         {
11             SortedList list = new SortedList();
12 
13             Array a = Enum.GetValues(t);
14             for (int i = 0; i < a.Length; i++)
15             {
16                 string enumName = a.GetValue(i).ToString();
17                 int enumKey = (int)System.Enum.Parse(t, enumName);
18                 string enumDescription = GetDescription(t, enumKey);
19                 list.Add(enumKey, enumDescription);
20             }
21             return list;
22         }
23 
24         private static string GetName(System.Type t, object v)
25         {
26             try
27             {
28                 return Enum.GetName(t, v);
29             }
30             catch
31             {
32                 return "未知";//UNKNOWN
33             }
34         }
35 
36 
37         /// <summary>
38         /// 返回指定枚举类型的指定值的描述
39         /// </summary>
40         /// <param name="t">枚举类型</param>
41         /// <param name="v">枚举值</param>
42         /// <returns></returns>
43         private static string GetDescription(System.Type t, object v)
44         {
45             try
46             {
47                 FieldInfo fi = t.GetField(GetName(t, v));
48                 DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
49                 return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
50             }
51             catch
52             {
53                 return "未知";
54             }
55         }
56         #endregion
57 
58         //状态
59         #region
60         //===========================================
61         public enum ProductStatus : int
62         {
63             [Description("已作废")]
64             Abandon = -1,
65             [Description("初始")]
66             Origin = 0,
67             [Description("已审核")]
68             Audit = 1,
69             [Description("已中断")]
70             Stop = 2
71 
72         }
73         public static SortedList GetProductStatus()
74         {
75             return GetStatus(typeof(ProductStatus));
76         }
77         public static string GetProductStatus(object v)
78         {
79             return GetDescription(typeof(ProductStatus), v);
80         }
81         //--------------------------------------------
82         #endregion
83     }
工具函数主要是为了方便以后在获取该枚举的时候使用的
比方我想把这个产品状态ProductStatus加载某个下拉框中
txtFocus_Type.Items.Clear();
                txtFocus_Type.DataSource = AppEnum.GetProductStatus();
                txtFocus_Type.DataValueField = "key";
                txtFocus_Type.DataTextField = "value";
                txtFocus_Type.DataBind();

  比方我从数据库中获取了该产品的状态,可能会是1(已审核)

AppEnum.GetProductStatus(Eval("ProductStatus"))

  在添加产品的时候,如果需要指定这个状态为初始的话

(int)AppEnum.ProductStatus.Origin

  

posted @ 2012-12-05 09:32  Nemo_Li  阅读(346)  评论(0编辑  收藏  举报