在C#编程中玩转枚举,分享我的EnumHelper。
在C#编程中玩转枚举,分享我的EnumHelper。
在软件开发过程中,我们经常会为特定的场景下的特定数据定义逻辑意义。比如在用户表中,我们可能会有一个用户状态字段,该字段为整形。如果该字段的值为1则代表用户状态正常,2则代表用户被锁定等等。这些规则应该被写入开发文档里,但是每次都去查文档,也是一件痛苦的事情。其实,在C#中有一个很简单的方法可以实现数据和表象意义之间的转换。枚举既是为此而生。
例如,我们有一个用户状态的枚举,它看起来像这个样子:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/// <summary>
/// 用户状态
/// </summary>
public enum UserState
{
/// <summary>
/// 未激活
/// </summary>
[Description("未激活")]
Nonactivated = 0,
/// <summary>
/// 正常
/// </summary>
[Description("正常")]
Normal = 1,
/// <summary>
/// 锁定
/// </summary>
[Description("锁定")]
Locked = 2
}
|
枚举名称用于区分类型,枚举值用于程序判断,Description特性专为显示而生。多么美好的配合。
C#
1
2
3
4
5
6
7
8
9
|
public static String GetDescription(Enum value)
{
Type type = value.GetType();
FieldInfo item = type.GetField(value.ToString(), BindingFlags.Public | BindingFlags.Static);
if (item == null) return null;
var attribute = Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null && !String.IsNullOrEmpty(attribute.Description)) return attribute.Description;
return null;
}
|
枚举除了用于定义和区分状态之外,也可以参与运算。基于枚举的位操作常常用于权限管理中。多个权限操作可以存储在同一个字段中,而不用在数据表中增加N多列,想想就觉得美好。一个常见的操作权限枚举定义如下:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/// <summary>操作权限</summary>
[Flags]
[Description("操作权限")]
public enum PermissionFlags
{
/// <summary>无权限</summary>
[Description("无")]
None = 0,
/// <summary>所有权限</summary>
[Description("所有")]
All = 1,
/// <summary>添加权限</summary>
[Description("添加")]
Insert = 2,
/// <summary>修改权限</summary>
[Description("修改")]
Update = 4,
/// <summary>删除权限</summary>
[Description("删除")]
Delete = 8,
/// <summary>自定义1权限</summary>
/// <remarks>这里没有接着排16,为了保留给上面使用</remarks>
[Description("自定义1")]
Custom1 = 0x20,
/// <summary>自定义2权限</summary>
[Description("自定义2")]
Custom2 = Custom1 * 2,
/// <summary>自定义3权限</summary>
[Description("自定义3")]
Custom3 = Custom2 * 2,
/// <summary>自定义4权限</summary>
[Description("自定义4")]
Custom4 = Custom3 * 2,
/// <summary>自定义5权限</summary>
[Description("自定义5")]
Custom5 = Custom4 * 2,
/// <summary>自定义6权限</summary>
[Description("自定义6")]
Custom6 = Custom5 * 2,
/// <summary>自定义7权限</summary>
[Description("自定义7")]
Custom7 = Custom6 * 2,
/// <summary>自定义8权限</summary>
[Description("自定义8")]
Custom8 = Custom7 * 2
}
|
如果我们要为用户设定添加和删除权限,只需要为用户的操作权限值设定为10即可(添加权限值为2,删除权限值为8,加起来值为10)。要验证用户是否包含某权限,只需要将该权限与用户拥有的权限值做位运算即可。
C#
1
2
3
4
5
6
7
8
9
10
|
var code = 10;
var flag = (PermissionFlags)code;
if ((PermissionFlags.Insert & flag) == flag)
{
Console.WriteLine("存在添加权限");
}
else
{
Console.WriteLine("没有添加权限");
}
|
在程序圈里摸爬滚打这几年,也勉为其难的步入“三流程序员”的行列。封装一下吧,要对得起自己学过的面向对象。(三流指:封装、继承、多态。)
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class EnumHelper
{
public static T Set<T>(this Enum source, T flag, Boolean value)
{
if (!(source is T)) throw new ArgumentException("枚举标识判断必须是相同的类型!", "source");
ulong s = Convert.ToUInt64(source);
ulong f = Convert.ToUInt64(flag);
if (value)
{
// 必须先检查是否包含这个标识位,因为异或的操作仅仅是取反
if ((s & f) != f) s ^= f;
}
else
s = s | f;
return (T)Enum.ToObject(typeof(T), s);
}
public static Boolean Has(this Enum value, Enum flag)
{
if (value.GetType() != flag.GetType()) throw new ArgumentException("枚举标识判断必须是相同的类型!", "flag");
ulong num = Convert.ToUInt64(flag);
return (Convert.ToUInt64(value) & num) == num;
}
public static String GetDescription(this Enum value)
{
Type type = value.GetType();
FieldInfo item = type.GetField(value.ToString(), BindingFlags.Public | BindingFlags.Static);
if (item == null) return null;
var attribute = Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null && !String.IsNullOrEmpty(attribute.Description)) return attribute.Description;
return null;
}
public static Dictionary<T, String> GetDescriptions<T>() where T : struct
{
return GetDescriptions<T>(typeof(T));
}
public static Dictionary<T, String> GetDescriptions<T>(Type type)
{
var dic = new Dictionary<T, String>();
foreach (FieldInfo item in type.GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (!item.IsStatic) continue;
var value = (T)item.GetValue(null);
string des = item.Name;
var dna = Attribute.GetCustomAttribute(item, typeof(DisplayNameAttribute)) as DisplayNameAttribute;
if (dna != null && !String.IsNullOrEmpty(dna.DisplayName)) des = dna.DisplayName;
var att = Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (att != null && !String.IsNullOrEmpty(att.Description)) des = att.Description;
dic.Add(value, des);
}
return dic;
}
}
|
但是,做到这里还不够,我们还需要更多的东西来支持界面显示。用于应付在Web开发中常用数据展示和筛选需求。当然,WinForm也可以,只不过需要看官自己去实现。
为WebForm扩展,用于在Repeater控件中展示:
C#
1
2
3
4
5
|
public static String GetDescription<T>(this Page page, Object value)
{
var t = (T)(Convert.ToInt32(value) as Object);
return (t as Enum).GetDescription();
}
|
调用示例:(前提是你得在Web.config中引入相应的命名空间)
XHTML
1
|
<td><%#this.GetDescription<UserState>( Eval("STATE")) %></td>
|
为DropDownList扩展绑定:
C#
1
2
3
4
5
6
7
8
9
10
|
public static void BindItem<T>(this DropDownList ddl, Boolean allowEmpty = false) where T : struct
{
var dic = Toolkit.Extension.EnumHelper.GetDescriptions<T>();
ddl.Items.Clear();
if (allowEmpty) ddl.Items.Add(new ListItem("==请选择==", String.Empty));
foreach (var item in dic)
{
ddl.Items.Add(new ListItem(item.Value, Convert.ToInt32(item.Key).ToString("D")));
}
}
|
调用示例:
C#
1
|
this.ddlState.BindItem<Common.Define.UserState>();
|
这些就是我工作以来在项目中使用枚举所带来的经验。这种做法大大的提高了编程的效率,可以让程序员更关注业务实现,而不必再为数据为0到底是什么意思扯皮。文中代码来自于真实项目,在你没有用错的情况下可以保证可用性。代码在很大程度上参考了X组件,再次对@大石头表示感谢。如果你感兴趣,可以来新生命团队做客。

最后编辑于:2014/6/12作者: Soar、毅
.NET 程序员,默默无闻的码农,一直希望行走的很文艺的苦逼青年.
在C#编程中玩转枚举,分享我的EnumHelper。的更多相关文章
- [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐
[翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...
- Python中模拟enum枚举类型的5种方法分享
这篇文章主要介绍了Python中模拟enum枚举类型的5种方法分享,本文直接给出实现代码,需要的朋友可以参考下 以下几种方法来模拟enum:(感觉方法一简单实用) 复制代码代码如下: # way1 ...
- 一文读懂高性能网络编程中的I/O模型
1.前言 随着互联网的发展,面对海量用户高并发业务,传统的阻塞式的服务端架构模式已经无能为力.本文(和下篇<高性能网络编程(六):一文读懂高性能网络编程中的线程模型>)旨在为大家提供有用的 ...
- 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用
今天学习了下scala中的链式调用风格的实现,在spark编程中,我们经常会看到如下一段代码: sc.textFile("hdfs://......").flatMap(_.spl ...
- perl编程中的map函数示例
转自:http://www.jbxue.com/article/14854.html 发布:脚本学堂/Perl 编辑:JB01 2013-12-20 10:20:01 [大 中 小] 本文介绍 ...
- Android编程中的实用快捷键
作为一个优秀的程序员,不但要能开发出漂亮的软件,也要能熟练掌握编程的技巧,包括IDE的快捷键使用.比如linux 下的VI编辑器,对于不熟练快捷键的人来说就是一个噩梦,但一旦你熟练了VI的快捷键,VI ...
- Java 数据类型在实际开发中应用二枚举
在实际编程中,往往存在着这样的"数据集",它们的数值在程序中是稳定的,而且"数据集"中的元素是有限的.在JDK1.5之前,人们用接口来描述这一种数据类型. 1. ...
- lazy ideas in programming(编程中的惰性思想)
lazy形容词,懒惰的,毫无疑问是一个贬义词.但是,对于计算机领域,lazy却是非常重要的优化思想:把任务推迟到必须的时刻,好处是避免重复计算,甚至不计算.本文的目的是抛砖引玉,总结一些编程中的laz ...
- (转)Attribute在.net编程中的应用
Attribute在.net编程中的应用(一)Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一 ...
随机推荐
- 解决无法切换到jenkins用户的问题
su - jenkins一直有效,今天在centos发现无效,原因是 /etc/password文件里的/bin/bash被yum安装的时候变成了/bin/false. 改动后就能够了. ubuntu ...
- 【iOS开发-21】UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解
(1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最以下,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...
- maple 教程
1 初识计算机代数系统Maple 1.1 Maple简说 1980年9月, 加拿大Waterloo大学的符号计算机研究小组成立, 開始了符号计算在计算机上实现的研究项目, 数学软件Maple是这个项目 ...
- UIButton 文字图片排列
UIButton缺省值是:图画-文字水平,所以我们并不需要调整. 1.写作-图画 水平显示,以前的文本,图片后再次 [btn setTitleEdgeInsets:UIEdgeInsetsMake(0 ...
- 但从谈论性能点SQL Server选择聚集索引键
简单介绍 在SQL Server中,数据是按页进行存放的.而为表加上聚集索引后,SQL Server对于数据的查找就是依照聚集索引的列作为keyword进行了. 因此对于聚集索引的选择对性能的影响就变 ...
- android Intent.createChooser 应用选择
在微博案例: 1.public void onClickShare(View view) { 2. 3. Intent intent=new Intent(Intent.ACTION_SEND); 4 ...
- Team Foundation Server 2015使用教程--团队项目删除
- WPF学习(10)模板
在前面一篇我们粗略说了Style和Behaviors,如果要自定义一个个性十足的控件,仅仅用Style和Behaviors是不行的,Style和Behaviors只能通过控件的既有属性来简单改变外观, ...
- net开源cms系统
.net开源cms系统推荐 内容目录: 提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,. ...
- App山寨疯狂 爱加密Apk加密平台防破解
App山寨疯狂 爱加密Apk加密平台防破解,Android系统由于其开源性,眼下已占领全球智能机近80%的市场,远超微软的WP系统和苹果的IOS系统.然而也正是由于开源性,Android盗版App在国 ...