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. Java8新特性——StreamAPI(一)

    1. 流的基本概念 1.1 什么是流? 流是Java8引入的全新概念,它用来处理集合中的数据,暂且可以把它理解为一种高级集合. 众所周知,集合操作非常麻烦,若要对集合进行筛选.投影,需要写大量的代码, ...

  2. 重温CLR(十二) 委托

    回调函数是一种非常有用的编程机制,它的存在已经有很多年了..NET通过委托来提供回调函数机制.不同于其他平台(比如非托管C++)的回调机制,委托的功能要多得多.例如,委托确保回调方法是类型安全的(这是 ...

  3. Django之Models

    1.数据库的配置 1    django默认支持sqlite,mysql, oracle,postgresql数据库.  <1> sqlite django默认使用sqlite的数据库,默 ...

  4. salesforce 公开网页,免登陆

    設定→カスタマイズ→コミュニテ→新建一个visfualforce的コミュニテ ビルド->カスタマイズ->コミュニティ->すべてのコミュニティ->新規コミュニティ 然后选中新规的 ...

  5. direct2D图片处理

    转自:http://blog.csdn.net/augusdi/article/details/9040177  Using Bitmap Brushes Direct2D 中的图片处理增加了很多的灵 ...

  6. YUYV&YV12&mtk6763

    stImgInOut.stImgInfo.enImageType = UV_IMAGE_TYPE_YV12; stImgInOut.stImgInfo.as32Pitch[0] = pStreamIm ...

  7. HTML 按钮换肤

    .button2{ background-image: url(images/input-bg.jpg); width: 83px; height: 31px; border: none 0px; f ...

  8. C/C++中一些不太注意到的小知识点--[锦集]

    C/C++中一些不太注意到的小知识点--[锦集] C/C++小知识点--[锦集] "="和"<=" 的优先级 1.( (file_got_len = re ...

  9. JAVA-Unit02: Oracle字符串操作 、 Oracle数值操作 、 Oracle日期操作 、 空值操作

    Unit02: Oracle字符串操作 . Oracle数值操作 . Oracle日期操作 . 空值操作 DQL数据查询语言 查询语句基本由SELECT子句由FROM子句构成. SELECT子句指定要 ...

  10. one2many &&many2many

    只记录双向的情况(双向是单向的一种)  @OneToMany 和 @ManyToOne :一个Group 包含多个 User; Group.class package com.XX.model; im ...