由于对英语的天生缺陷,在枚举时一直使用中文,这样就不用看注释就知道枚举意思,今天看到博文

https://www.cnblogs.com/emrys5/p/Enum-rename-htmlhelper.html使用特性代替了直接使用中文作为属性。特意摘抄部分为以后使用方便

枚举特性类:

    /// <summary>
/// 枚举特性
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class DescriptionAttribute : Attribute
{
/// <summary>
/// 排序
/// </summary>
public int Order { get; set; } /// <summary>
/// 名称
/// </summary>
public string Name { get; set; } /// <summary>
/// 定义描述名称
/// </summary>
/// <param name="name">名称</param>
public DescriptionAttribute(string name)
{
Name = name;
} /// <summary>
/// 定义描述名称和排序
/// </summary>
/// <param name="name">名称</param>
/// <param name="order">排序</param>
public DescriptionAttribute(string name, int order)
{
Name = name;
Order = order;
} }

枚举帮助类:

     /// <summary>
/// 枚举帮助类
/// </summary>
public static class EnumTools
{
/// <summary>
/// 获取当前枚举值的描述和排序
/// </summary>
/// <param name="value"></param>
/// <returns>返回元组Tuple(string,int)</returns>
public static Tuple<string, int> GetDescription(this Enum value)
{
int order = ;
string description = string.Empty; Type type = value.GetType(); // 获取枚举
FieldInfo fieldInfo = type.GetField(value.ToString()); // 获取枚举自定义的特性DescriptionAttribute
object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute); description = fieldInfo.Name; if (attr != null)
{
order = attr.Order;
description = attr.Name;
}
return new Tuple<string, int>(description, order); } /// <summary>
/// 获取当前枚举的所有描述
/// </summary>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetAll<T>()
{
return GetAll(typeof(T));
} /// <summary>
/// 获取所有的枚举描述和值
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static List<KeyValuePair<int, string>> GetAll(Type type)
{ List<EnumToolsModel> list = new List<EnumToolsModel>(); // 循环枚举获取所有的Fields
foreach (var field in type.GetFields())
{
// 如果是枚举类型
if (field.FieldType.IsEnum)
{
object tmp = field.GetValue(null);
Enum enumValue = (Enum)tmp;
int intValue = Convert.ToInt32(enumValue);
var dec = enumValue.GetDescription();
int order = dec.Item2;
string showName = dec.Item1; // 获取描述和排序
list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order });
}
} // 排序并转成KeyValue返回
return list.OrderBy(i => i.Order).Select(i => new KeyValuePair<int, string>(i.Key, i.Name)).ToList(); }
/// <summary>
/// 枚举Model
/// </summary>
partial class EnumToolsModel
{
public int Order { get; set; }
public string Name { get; set; }
public int Key { get; set; }
} }

把原文中的out参数替换成返回元组,由于项目是vs2015开发,不能用c#7.0特性,否则用7.0中的值元组应该更好一点。性能和显示友好性都会有改进。

Enum扩展特性,代替中文属性的更多相关文章

  1. GridView中文属性

    GridControl的中文属性: 1  Appearance 外观 Appearance 外观设置 ColumnFilterButton  行过滤器按钮 BackerColor  背景色 Backe ...

  2. JS 4 新特性:混合属性(mixins)

    Ext JS4的新特征1:混合属性(mixins) 组合是Extjs4的新特性,可用于实现多继承的情况.该属性会以同步方式加载类文件,并实例化该类(译者推理其内部使用Ext.create方法).直接上 ...

  3. C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)

    既然是一个窗体设计器,那就应该能够设置控件的属性,设置属性最好的当然是PropertyGrid了,我们仅仅需要使用一个PropertyGrid.SelectedObject = Control就可以搞 ...

  4. x:Name标记特性与Name属性

    本文转载自silvergingko的专栏 在Xaml中定义了一个元素后,如果后面要使用该元素,则必须为该元素定义一个元素名称,在随后的Xaml中,通过元素名称来使用该元素. 在Xaml中,元素的名称定 ...

  5. MVC4 数据验证、特性、自动属性总结

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    最近在做自学MVC,遇到的问题很多,索性一点点总结 ...

  6. GDAL C#中文路径,中文属性名称乱码问题

    昨天写的博客,将C#读取shp中文属性值乱码的问题应该可以解决,博客地址为:http://blog.csdn.net/liminlu0314/article/details/54096119,然后又测 ...

  7. DOM元素的Attribute(特性)和Property(属性) 【转载】

    1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...

  8. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  9. GDAL支持中文路径和Shp文件中文属性写入

    在使用GDAL的过程中,为了支持中文,比需手动进行中文路径的设置,同时特别是在对Shp的属性进行中文输入的时候,都必须进行必要的设定. 为了支持中文路径,在注册了驱动之后,加上第三句就可以了.必须设置 ...

随机推荐

  1. 一、selenium 环境搭建

    本教程演示是在window系统上演示,linux.mac 系统以后会更新. 1.准备工作 1.python2或者python3安装包,官网:https://www.python.org/downloa ...

  2. log4j.properties配置与将异常输出到Log日志文件实例

    将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...

  3. Mybaits API docs

    http://www.mybatis.org/mybatis-3/apidocs/index.html

  4. Unity - Photon PUN 本地与网络同步的逻辑分离 (一)

    服务器大家可以使用Photon官网提供的,这样会变得很简单,直接搭建下就好.或者下载到本地开启本地端Photon服务器 (大家也可以使用和我一样方式有时间做了个winform 程序用来管理本地服务器开 ...

  5. Context 解析

    ·  ContextWrapper比较有意思,其在SDK中的说明为“Proxying implementation ofContext that simply delegates all of its ...

  6. 1111. Online Map (30)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  7. eclipse下Spring环境构建及插件

    首先获取spring tool suite插件 获取地址http://spring.io/tools/sts/ 然后打开eclipse选择菜单栏Help下Install new software添加我 ...

  8. python中使用XPath

    XPath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. XPath介绍: ...

  9. Error resolving template [xxx], template might not exist or might not be exist

    Springboot+thymeleaf+mybatis 抛Error resolving template [xxx], template might not exist的异常 原因是我们在pom. ...

  10. Unity3D编辑器扩展(二)——定义自己的窗口

    上一篇我们讲了如何定义菜单按钮 https://www.cnblogs.com/xiaoyulong/p/10115053.html 这一篇我们讲如何定义自己的窗口. 定义窗口我们需要继承 Edito ...