在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是一 ...
随机推荐
- App如何选择移动广告平台,开发者2 - 移动变现模式分析
开发人员社区的上述分析.它可能无法覆盖全部,但是,每一个开发者都需要根据自己的特点变现模式选择.继App流动性模式做了全面的分析. 游戏.广告.电商是互联网的三种变现模式,移植到移动互联网也相同适用. ...
- LINQ之路系列文章导读
本系列文章将会分为3篇来进行阐述,如下: LINQ之路(1):LINQ基础 LINQ之路(2):LINQ to SQL本质 LINQ之路(3):LINQ扩展
- Nancy
Nancy Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono.Nancy的整套设计理念是基于"super-duper-happy ...
- android 原生应用、Web应用、混合应用优缺点分析
近期开发几个项目,牵涉到android的几种开发模式.对于原生态开发.web 应用开发以及混合模式开发,本人觉得并非哪一种就是最好的,哪一种就是最差的,这个全然是依据项目的实际需求,选择一种合适的开发 ...
- nginx: [emerg] the size 10485760 of shared memory zone "cache_one" conflicts with already declared size 0
注意配置段中的区域包含关系. proxy_cache_patch 要在proxy_cache前已经定义. what seems to be the problem? [emerg]: the size ...
- BZOJ 1009 HNOI2008 GT考试 KMP算法+矩阵乘法
标题效果:给定的长度m数字字符串s.求不包括子s长度n数字串的数目 n<=10^9 看这个O(n)它与 我们不认为这 令f[i][j]长度i号码的最后的字符串j位和s前者j数字匹配方案 例如,当 ...
- mahout安装和测试
Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发者更加方便快捷地创建智能应用程序.Apac ...
- UVa 699 落叶
意甲冠军:我几乎不记得的题意!.! 它是一个长坑..我们从根节点开始,留下每一步,保存横坐标1,正确的一步加上横坐标1. 那么同样的横坐标统计data值总和. 思维:我来想,这可以递归成就.上就能够算 ...
- 软测试综述——PV操作
在操作系统中,进程之间常常会存在相互排斥(都须要共享独占性资源时)和同步(完毕异步的两个进程的协作)两种关系.而信号量和PV操作完美有效的处理了这两种情况. 相互排斥:就好比过独木桥,一 ...
- hibernate学习笔记(1)hibernate基本步骤
hibernate基本步骤 1.创hibernate置对象 Configuration config = newConfiguration(); config.configure("hibe ...