很多朋友认为设置枚举还不容易,如以下代码就OK了,
01 |
public enum Test
|
02 |
{ |
03 |
/// <summary>
|
04 |
/// 拒绝
|
05 |
/// </summary>
|
06 |
Deny = 0,
|
07 |
/// <summary>
|
08 |
/// 删除
|
09 |
/// </summary>
|
10 |
Del = 1,
|
11 |
/// <summary>
|
12 |
/// 修改
|
13 |
/// </summary>
|
14 |
Modify = 2,
|
15 |
/// <summary>
|
16 |
/// 添加
|
17 |
/// </summary>
|
18 |
Add = 3,
|
19 |
/// <summary>
|
20 |
/// 查询
|
21 |
/// </summary>
|
22 |
Search = 4
|
23 |
} |
我们看看上面的代码,这样的设置的确没错,但确没有考虑到一个复用性的效果。
很多时候,我们能够看到WINDOW的一个枚举能够并用,如:Test t=Test.Add|Test.Del,等等
那么我们怎么样才能设置这样的枚举呢?下面我们来看一段枚举代码:
01 |
[Flags] |
02 |
public enum Popendom : int
|
03 |
{ |
04 |
/// <summary>
|
05 |
/// 拒绝
|
06 |
/// </summary>
|
07 |
Deny = 1,
|
08 |
/// <summary>
|
09 |
/// 删除
|
10 |
/// </summary>
|
11 |
Del = 2,
|
12 |
/// <summary>
|
13 |
/// 修改
|
14 |
/// </summary>
|
15 |
Modify = 4,
|
16 |
/// <summary>
|
17 |
/// 添加
|
18 |
/// </summary>
|
19 |
Add = 8,
|
20 |
/// <summary>
|
21 |
/// 查询
|
22 |
/// </summary>
|
23 |
Search = 16
|
24 |
} |
1、[Flags]
在C#的枚举中:Flags属性很重要,因为她可以将你的枚举中的类型转成字符串。
2、public enum Popendom : int
这个枚举继承的int类型,也就是起到约束的作用。
3、枚举设置值,很多认为里面的值是可以随意设置的,但其实里面有个设置技巧,视项目的需求不同的。
在设置可并用的枚举我们需要一个原则就是累加值不能在枚举设置值中出现
Deny =1,Del = 2,Modify = 4等等
(0)+1=1,
1+2=(3)
1+2+4=(7)
(值)不能在枚举中出现。也就是I*2
这样我们就能实现了
Popendom p=Popendom.Del|Popendom.Add|Popendom.Modify等等了。
01 |
public MainPage()
|
02 |
{ |
03 |
InitializeComponent();
|
04 |
Popendom ps = Popendom.Add | Popendom.Del | Popendom.Modify | Popendom.Search | Popendom.Deny;
|
05 |
IList<Popendom> list = GetPopendomList(ps.ToString());
|
06 |
bool b = HasThisPopendom(ps.ToString(), Popendom.Del);
|
07 |
} |
08 |
09 |
[Flags] |
10 |
public enum Popendom : int
|
11 |
{ |
12 |
/// <summary>
|
13 |
/// 拒绝
|
14 |
/// </summary>
|
15 |
Deny = 1,
|
16 |
/// <summary>
|
17 |
/// 删除
|
18 |
/// </summary>
|
19 |
Del = 2,
|
20 |
/// <summary>
|
21 |
/// 修改
|
22 |
/// </summary>
|
23 |
Modify = 4,
|
24 |
/// <summary>
|
25 |
/// 添加
|
26 |
/// </summary>
|
27 |
Add = 8,
|
28 |
/// <summary>
|
29 |
/// 查询
|
30 |
/// </summary>
|
31 |
Search = 16
|
32 |
} |
33 |
34 |
public enum Test
|
35 |
{ |
36 |
/// <summary>
|
37 |
/// 拒绝
|
38 |
/// </summary>
|
39 |
Deny = 0,
|
40 |
/// <summary>
|
41 |
/// 删除
|
42 |
/// </summary>
|
43 |
Del = 1,
|
44 |
/// <summary>
|
45 |
/// 修改
|
46 |
/// </summary>
|
47 |
Modify = 2,
|
48 |
/// <summary>
|
49 |
/// 添加
|
50 |
/// </summary>
|
51 |
Add = 3,
|
52 |
/// <summary>
|
53 |
/// 查询
|
54 |
/// </summary>
|
55 |
Search = 4
|
56 |
} |
57 |
58 |
public IList<Popendom> GetPopendomList(string popendoms)
|
59 |
{ |
60 |
List<Popendom> list = new List<Popendom>();
|
61 |
string[] ps = GetPopendoms(popendoms);
|
62 |
foreach (string p in ps)
|
63 |
{
|
64 |
switch (p)
|
65 |
{
|
66 |
case "Deny": list.Add(Popendom.Deny); break;
|
67 |
case "Del": list.Add(Popendom.Del); break;
|
68 |
case "Modify": list.Add(Popendom.Modify); break;
|
69 |
case "Add": list.Add(Popendom.Add); break;
|
70 |
case "Search": list.Add(Popendom.Search); break;
|
71 |
}
|
72 |
}
|
73 |
return list;
|
74 |
} |
75 |
76 |
public bool HasThisPopendom(string popendoms, Popendom p)
|
77 |
{ |
78 |
return GetPopendoms(popendoms).Contains(p.ToString());
|
79 |
} |
80 |
81 |
public string[] GetPopendoms(string popendoms)
|
82 |
{ |
83 |
return popendoms.Replace(" ", "").Split(',');
|
84 |
} |