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#枚举最优雅的用法的更多相关文章

  1. 【转】Java 枚举7常见种用法

    原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu  原创地址:http://blog.li ...

  2. Java中枚举的写法和用法

            在公司代码中,用了一大堆的枚举,看得我好懵逼.下面开始看看枚举怎么写和怎么用. 一.枚举的写法         关于枚举的写法,网上好多这方面的知识.这里直接贴一个我自己写的枚举类的代 ...

  3. Java 枚举7常见种用法

    DK1.5引入了新的类型--枚举.在 Java 中它虽然算个"小"功能,却给我的开发带来了"大"方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是:  ...

  4. Java 枚举7种常见用法

    (转)原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型--枚举.在 Java ...

  5. Java枚举常见7种用法

    DK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便.用法一:常量在JDK1.5 之前,我们定义常量都是: publicstaticfianl…… .现 ...

  6. Java枚举7常见种用法

    DK1.5引入了新的类型——枚举.在Java中它虽然算个“小”功能,却给我的开发带来了“大”方便. 方法/步骤 用法一:常量 在JDK1.5之前,我们定义常量都是:publicstaticfianl. ...

  7. Java 枚举7常见种用法(转)

    JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl ...

  8. 浅谈在Java开发中的枚举的作用和用法

    枚举(enum),是指一个经过排序的.被打包成一个单一实体的项列表.一个枚举的实例可以使用枚举项列表中任意单一项的值.枚举在各个语言当中都有着广泛的应用,通常用来表示诸如颜色.方式.类别.状态等等数目 ...

  9. Java 枚举常见7种用法

    用法一:常量 在JDK1.5 之前,我们定义常量都是: publicstaticfianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. publ ...

随机推荐

  1. 接口测试基础——第6篇unittest模块(一)

    我们先来简单介绍一下unittest框架,先上代码,跟住了哦~~ 1.建立如下结构的文件夹: 注意,上面的文件夹都是package,也就是说你在new新建文件夹的时候不要选directory,而是要选 ...

  2. python 中的异常处理

    refer to: http://www.runoob.com/python/python-exceptions.html http://www.pythondoc.com/pythontutoria ...

  3. .net SMTP 发送邮件

    using System.Net.Mail; public static bool SendMail(string messTo,string messBody) { MailMessage mess ...

  4. [MEF]第03篇 MEF延迟加载导出部件及元数据

    一.演示概述此演示介绍了MEF的延迟加载及元数据相关的内容.在实际的设计开发中,存在着某些对象是不需要在系统运行或者附属对象初始化的时候进行实例化的,只需要在使用到它的时候才会进行实例化,这种方式就可 ...

  5. CF 360E Levko and Game——贪心

    题目:http://codeforces.com/contest/360/problem/E 官方题解与证明:http://codeforces.com/blog/entry/9529 一条可以调整的 ...

  6. VS2010 无法启动程序,系统找不到指定的文件

    1>------ 已启动生成: 项目: work, 配置: Debug Win32 ------1>生成启动时间为 2018/1/9 14:01:16 下午.1>Initialize ...

  7. 配置Jar包及相关依赖Jar包的本地存放路径

    配置Jar包及相关依赖Jar包的本地存放路径 用 maven2 ,pom.xml中设置了依赖,会帮你下载所有依赖的.jar到 M2_REPO 指向的目录. M2_REPO是一个用来定义 maven 2 ...

  8. 双口RAM,值得研究

    在FPGA设计过程中,使用好双口RAM,也是提高效率的一种方法. 官方将双口RAM分为简单双口RAM和真双口RAM. 简单双口RAM只有一个写端口,一个读端口. 真双口RAM分别有两个写端口和两个读端 ...

  9. ThreadPoolExecutor之三:自定义线程池-扩展示例

    ThreadPoolExecutor是可扩展的,下面一个示例: package com.dxz.threadpool.demo1; import java.util.concurrent.Blocki ...

  10. setTimeout和setInterval的unref()和ref()用法

    var testFunction=function(){ console.log("guoyansi"); } var timer=setInterval(testFunction ...