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. 使用python处理selenium中的获取文本问题

    # 获取文本 button_name = self.driver.find_element_by_id("sign_in_display").text

  2. maven的介绍以及使用

    maven的介绍以及使用 1.什么是maven maven是一个项目管理工具,一个依赖管理系统,maven通过项目对象模型来管理jar包(POM.xml文件) 2.maven的优点 1.maven使用 ...

  3. XML 的 XPath 语法

    XPath 是 XML 路径语言(XML Path Language),用来确定XML文档中某部分位置的语言.无论是什么语言什么框架,几乎都可以使用 XPath 来高效查询 XML 文件. 本文将介绍 ...

  4. ubnut16.4 下 设置静态ip

    一.编辑interfaces sudo vi /etc/network/interface ### i 键进入编辑 二.编辑该文件 auto loiface lo inet loopbackauto ...

  5. [BZOJ5338][TJOI2018]xor

    bzoj luogu descirption 现在有一棵以 \(1\) 为根节点的由 \(n\) 个节点组成的树,树上每个节点上都有一个权值 \(v_i\) .现在有 \(Q\) 次操作,操作如下: ...

  6. 开启opcache提高性能

    在开启opcache之前,我们先介绍一下编译与解释: 编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快:而解释器则是只在执行程 ...

  7. 【java基础】java中String的注意点

    [java的内存模型] 一.Java内存模型 按照官方的说法:Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配. JVM主要管理两种类型内存:堆和非堆,堆内存(Hea ...

  8. caddy server && caddyfile

    This page describes how to configure Caddy using the Caddyfile. Introduction The term "Caddyfil ...

  9. Quick 3.3 的代码资源加密

    http://cn.cocos2d-x.org/tutorial/show?id=1507 http://cn.cocos2d-x.org/tutorial/show?id=1447 http://b ...

  10. warning: backslash and newline separated by space [enabled by default]

    警告:反斜杠和换行符之间多了空格. 这种问题出现在宏定义 #define,并且有多行,每行之间要用 “\” 连接起来. 解决办法:删除 “\” 后面的空格,直接紧跟回车.