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 ...
随机推荐
- bootstrap 折叠菜单
首先从 左侧的折叠菜单 开始.看图. 2. CSS 代码 以下是自定义的css代码,由于系统是内部使用,所以优先考虑chrome,firefox 不考虑IE了. #main-nav { margin- ...
- 定义文档兼容性、让IE按指定版本解析我们的页面
http://blog.useasp.net/archive/2013/05/29/x-UA-compatible-defining-document-compatibility-emulate-ie ...
- 实例-系数可配置的fir滤波器
- nodejs之fs模块
nodejs中的file system文件系统模块 1.文件的读取readFile //引入文件系统模块 const fs = require('fs'); //文件读取是异步操作 fs.readFi ...
- 10.Python运行Scrapy时出现错误: ModuleNotFoundError: No module named 'win32api'
1.在命令行输入:scrapy crawl demo(demo为爬虫标识,是唯一的) 2.报错信息如下所示: 3.解决方法:https://github.com/mhammond/pywin32/re ...
- linux常用小技巧(持续更新中)
一.设置固定ip地址1.config查看用的是哪一个网卡这是假设用的是eth12.修改dns地址vim /etc/resolv.confsearch 域名地址nameserver 192.168.3. ...
- 在SpringMVC中使用@RequestBody和@ResponseBody注解处理json时,报出HTTP Status 415的解决方案
我在使用SpringMVC的@RequestBody和@ResponseBody注解处理JSON数据的时候,总是出现415的错误,说是不支持所提交数据格式,我在页面中使用了JQuery的AJAX来发出 ...
- 【BZOJ】2809: [Apio2012]dispatching(左偏树)
题目 传送门:QWQ 分析 显然是一个资瓷合并的堆 现学了一发左偏树:教程 然后就没了 代码 #include <bits/stdc++.h> #define lc son[x][0] # ...
- CDH5.10 添加kafka服务
简介: CDH的parcel包中是没有kafka的,kafka被剥离了出来,需要从新下载parcel包安装.或者在线安装,但是在线安装都很慢,这里使用下载parcel包离线安装的方式. PS:kafk ...
- angularJs中的发送请求例子
$http({ //发送请求 url: 'http://localhost:8080/teacher/api/login', method: 'post', data: obj }) .success ...