在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是一 ...
随机推荐
- ORACLE 实验一
实验一:数据定义 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.熟悉Oracle的client配置: 2.掌握SQL Plus的使用: 3.掌握SQL模式定义语句,定义相关的表 ...
- SSL探03
本文探讨了Openssl的Engine机械.Openssl硬件引擎(Engine)可以使用户比較easy地将自己的硬件增加到openssl中去,替换其提供的软件算法. ENGINE 是 OPENSSL ...
- IOC/DI的基本思想
IOC/DI的基本思想 1.把程序之间的依赖关系去掉 2.把程序对象设置到IOC/DI容器的配置中作为Bean 3.由IOC/D.容器来管理Bean的创建和实例化 4.由IOC/DI容器来把Bean之 ...
- 数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件
原文:数据库管理--Powershell--使用Powershell脚本找出消耗最多磁盘空间的文件 原文译自: http://www.mssqltips.com/sqlservertip/2774/p ...
- hdu1381 Crazy Search(hash map)
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...
- Sqlserver 高并发和大数据存储方案
Sqlserver 高并发和大数据存储方案 随着用户的日益递增,日活和峰值的暴涨,数据库处理性能面临着巨大的挑战.下面分享下对实际10万+峰值的平台的数据库优化方案.与大家一起讨论,互相学习提高! ...
- Visual Studio Team Services使用教程--Readers tfs组成员添加
- 深入浅出java并发
http://www.blogjava.net/xylz/archive/2010/07/08/325587.html
- ORDER BY RAND()
大概是因为需要研究了一下MYSQL随机样本实现.例如:离tablename表随机抽取了创纪录,我们一般的写法是:SELECT * FROM tablename ORDER BY RAND() LIMI ...
- hexo 部署至Git遇到的坑
查找资料的时候发现了next这个博客主题,next!非常的漂亮,顺手查看了hexo的相关部署. Hexo官方介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲 ...