C#枚举最优雅的用法
public enum AbilityLevel
{
/// <summary>
/// Indicates that the individual has a general knowledge to a certain technology, it is the lowest level.
/// </summary>
[Description("入门")]
General = 1, /// <summary>
/// Indicates that the individual comprehends a certain technology.
/// </summary>
[Description("了解")]
Comprehend = 2, /// <summary>
/// Indicates that the individual has a pratical knowledge to a certain technology.
/// </summary>
[Description("一般")]
Pratical = 3, /// <summary>
/// Indicates that the individual is very skillful to a certain technology.
/// </summary>
[Description("良好")]
Skilled = 4, /// <summary>
/// Indicates that the individual has a great knowledge and masters a certain technology, it is the highest level.
/// </summary>
[Description("精通")]
Master = 5
}
拓展方法,或者说是重写ToString()方法
/// <summary>
/// Provides globalization for <see cref="AbilityLevel"/> enum.
/// </summary>
public static class AbilityLevelValue
{
/// <summary>
/// Gets the culture specified string value of <see cref="AbilityLevel"/>.
/// </summary>
/// <param name="level">The value of <see cref="AbilityLevel"/>.</param>
/// <param name="cultureName">The short name of culture.</param>
/// <returns>Culture specified string value of <paramref name="level"/></returns>
public static string ToLocalString(this AbilityLevel level, string cultureName="zh-cn")
{
if (cultureName == "zh-cn")
{
switch (level)
{
case AbilityLevel.General:
return "入门";
case AbilityLevel.Comprehend:
return "了解";
case AbilityLevel.Pratical:
return "一般";
case AbilityLevel.Skilled:
return "良好";
case AbilityLevel.Master:
return "精通";
default:
throw new ArgumentException(
"Invalid AbilityLevel value...", "level");
}
}
else
{
return level.ToString();
}
}
}
获取特性,自定义了一个枚举的拓展方法
/// <summary>
/// 枚举拓展类
/// </summary>
public static class EnumExt
{
public static string GetEnumDescription( this System.Enum enumObj)
{
System.Reflection.FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString()); object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return String.Empty;
}
else
{
var attrib = attribArray[0] as DescriptionAttribute; return attrib.Description;
}
}
}
重点在下边
public static SelectList ToSelectList<TEnum>(this TEnum enumObj, Func<TEnum, string> getDesc)
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
where getDesc(e) != null
select new { ID = e, Name = getDesc(e) }; return new SelectList(values, "ID", "Name", enumObj);
}
用法在这里
<p>
<label>公司性质<em>*</em>:</label>
@Html.DropDownListFor(m => m.LegalStatus,
Model.LegalStatus.ToSelectList(e => e.GetEnumDescription()),
new { @class = "default" })
@Html.ValidationMessageFor(m => m.LegalStatus)
</p>
运行结果

C#枚举最优雅的用法的更多相关文章
- 【转】Java 枚举7常见种用法
原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu 原创地址:http://blog.li ...
- Java中枚举的写法和用法
在公司代码中,用了一大堆的枚举,看得我好懵逼.下面开始看看枚举怎么写和怎么用. 一.枚举的写法 关于枚举的写法,网上好多这方面的知识.这里直接贴一个我自己写的枚举类的代 ...
- Java 枚举7常见种用法
DK1.5引入了新的类型--枚举.在 Java 中它虽然算个"小"功能,却给我的开发带来了"大"方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: ...
- Java 枚举7种常见用法
(转)原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型--枚举.在 Java ...
- Java枚举常见7种用法
DK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便.用法一:常量在JDK1.5 之前,我们定义常量都是: publicstaticfianl…… .现 ...
- Java枚举7常见种用法
DK1.5引入了新的类型——枚举.在Java中它虽然算个“小”功能,却给我的开发带来了“大”方便. 方法/步骤 用法一:常量 在JDK1.5之前,我们定义常量都是:publicstaticfianl. ...
- Java 枚举7常见种用法(转)
JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl ...
- 浅谈在Java开发中的枚举的作用和用法
枚举(enum),是指一个经过排序的.被打包成一个单一实体的项列表.一个枚举的实例可以使用枚举项列表中任意单一项的值.枚举在各个语言当中都有着广泛的应用,通常用来表示诸如颜色.方式.类别.状态等等数目 ...
- Java 枚举常见7种用法
用法一:常量 在JDK1.5 之前,我们定义常量都是: publicstaticfianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. publ ...
随机推荐
- LOJ2324. 「清华集训 2017」小 Y 和二叉树【贪心】【DP】【思维】【好】
LINK 思路 首先贪新的思路是处理出以一个节点为根所有儿子的子树中中序遍历起始节点最小是多少 然后这个可以两次dfs来DP处理 然后就试图确定中序遍历的第一个节点 一定是siz<=2的编号最小 ...
- Visual->UIElement->FrameworkElement,带来更多功能的同时也带来了更多的限制
在 WPF 或 UWP 中,我们平时开发所遇到的那些 UI 控件或组件,都直接或间接继承自 Framework.例如:Grid.StackPanel.Canvas.Border.Image.Butto ...
- python环境搭建-Linux系统下python2.7升级python3.5.2步骤
首先Python 查看版本 , 在Linux下特别注意权限问题,创建目录时候切记给予权限 如果是 ubnutu 请使用首先切换到 sudo su , 否则 make install 会出现问题.. 升 ...
- CMDB
一.CMDB CMDB --Configuration Management Database 配置管理数据库, CMDB存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧 ...
- UVA11401 Triangle Counting
题意 输入\(n\),输出有多少种方法可以从\(1,2,3,\dots,n\)中选出3个不同的整数,使得以他们为三边长可以组成三角形. \(n \leq 10^6\) 分析 参照刘汝佳的题解. 按最大 ...
- php查询mysql数据库 查询条件替中文字符串变量时无法查询
$temp2 ='十年';mysql_query("SET NAMES GBK"); $res = mysql_query("select songer_name fro ...
- Unit01: Web概述 、 HTML概述 、 文本处理 、 图像和超链接 、 表格 、 表单
Unit01: Web概述 . HTML概述 . 文本处理 . 图像和超链接 . 表格 . 表单 demo1.html <!-- 声明网页的版本(文档类型) --> <!doctyp ...
- 队列之blah集合
做了一个NOI上面的问题,叫blah集合,以a为基数,则2x+1和3x+1都在集合中,且集合中全部元素都由此计算得来.a∈[1,50],问升序排列后第n(n∈[1,1000000])个元素是多少.以输 ...
- TCP之二:TCP的三次握手与四次分手
一.TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文章时,我想你也知道TCP的概念了,想要更深入的了解TCP的工作,我们就继续.它只是一个超级麻烦的协议,而它又是互联网的基础 ...
- ROS6.16开始支持802.11ac了,扫盲下
Wi-Fi的5G频段与802.11AC背后那些事儿本文章来自某路由论坛,作者为张导,本人转载,原地址http://bbs.hiwifi.com/thread-9086-1-1.html 曾几何时,大家 ...