一般情况我们会用枚举类型来存储一些状态信息,而这些信息有时候需要在前端展示,所以需要展示中文注释描述。

为了方便获取这些信息,就封装了一个枚举扩展类。

    /// <summary>
/// 枚举扩展类
/// </summary>
public static class EnumExtension
{
/// <summary>
/// 获取枚举的描述,需要DescriptionAttribute属性
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static string GetDescription(this Enum e)
{
//获取枚举的Type类型对象
var type = e.GetType();
//获取枚举的所有字段
var fields = type.GetFields(); //遍历所有枚举的所有字段
foreach (var field in fields)
{
if (field.Name != e.ToString())
{
continue;
}
//第二个参数true表示查找EnumDisplayNameAttribute的继承链 if (field.IsDefined(typeof(DescriptionAttribute), true))
{
var attr = field.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
} //如果没有找到自定义属性,直接返回属性项的名称
return e.ToString();
} /// <summary>
/// 根据枚举获取下拉框列表
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static List<ComboboxItemDto> GetSelectList(this Enum en)
{
var list = new List<ComboboxItemDto>(); foreach (var e in Enum.GetValues(en.GetType()))
{
list.Add(new ComboboxItemDto() { DisplayText = GetDescription(e as Enum), Value = ((int)e).ToString(), IsSelected = e == en });
} return list;
} /// <summary>
/// 根据枚举获取下拉框列表
/// </summary>
/// <param name="type">枚举类型</param>
/// <returns></returns>
public static List<ComboboxItemDto> GetSelectList(this Type type)
{
var list = new List<ComboboxItemDto>(); foreach (var e in Enum.GetValues(type))
{
list.Add(new ComboboxItemDto() { DisplayText = GetDescription(e as Enum), Value = ((int)e).ToString() });
} return list;
}
}

上面代码中的 ComboboxItemDto 类是来自 Abp 源码,它主要用于提供前端下拉框的数据源。

    //
// 摘要:
// This DTO can be used as a simple item for a combobox/list.
public class ComboboxItemDto
{
//
// 摘要:
// Creates a new Abp.Application.Services.Dto.ComboboxItemDto.
public ComboboxItemDto();
//
// 摘要:
// Creates a new Abp.Application.Services.Dto.ComboboxItemDto.
//
// 参数:
// value:
// Value of the item
//
// displayText:
// Display text of the item
public ComboboxItemDto(string value, string displayText); //
// 摘要:
// Value of the item.
public string Value { get; set; }
//
// 摘要:
// Display text of the item.
public string DisplayText { get; set; }
//
// 摘要:
// Is selected?
public bool IsSelected { get; set; }
}

好了,下面来举个栗子,这是一个订单枚举类

    /// <summary>
/// 商品订单状态
/// </summary>
public enum CommodityOrderState
{
/// <summary>
/// 待付款
/// </summary>
[Description("待付款")]
PendingPay,
/// <summary>
/// 待发货
/// </summary>
[Description("待发货")]
PendingShip,
/// <summary>
/// 待收货
/// </summary>
[Description("待收货")]
PendingReceipt,
/// <summary>
/// 待评价
/// </summary>
[Description("待评价")]
PendingEvaluation,
/// <summary>
/// 已评价
/// </summary>
[Description("已评价")]
Evaluated,
/// <summary>
/// 已退款
/// </summary>
[Description("已退款")]
Refunded = 100
}

这是一个订单DTO,一般会存在订单状态字段,就像这样。

        /// <summary>
/// 订单状态(这个字段会通过AutoMapper自动映射)
/// </summary>
public CommodityOrderState State { get; set; }
/// <summary>
/// 订单状态描述
/// </summary>
public string StateDesc => State.GetDescription();

好了,这样前端就能拿到订单状态描述信息了,是不是很方便。

.net工具类 获取枚举类型的描述的更多相关文章

  1. 获取枚举类型的描述description

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  2. 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。

    1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...

  3. Spring普通类/工具类获取并调用Spring service对象的方法

    参考<Spring普通类获取并调用Spring service方法>,网址:https://blog.csdn.net/jiayi_0803/article/details/6892455 ...

  4. 获取枚举类型Description特性的描述信息

    C#中可以对枚举类型用Description特性描述. 如果需要对Description信息获取,那么可以定义一个扩展方法来实现.代码如下: public static class EnumExten ...

  5. C# 枚举类型的描述信息获取

    新建一个控制台方法,写好自己的枚举类型: 如图: 在里面添加获取描述的方法: 具体源码: 链接:http://pan.baidu.com/s/1nv4rGkp 密码:byz8

  6. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  7. Android自定义工具类获取按钮并绑定事件(利用暴力反射和注解)

    Android中为按钮绑定事件的有几种常见方式,你可以在布局文件中为按钮设置id,然后在MainActivity中通过findViewById方法获取按钮对象实例,再通过setOnClickListe ...

  8. Java基础学习(五)-- Java中常用的工具类、枚举、Java中的单例模式之详解

    Java中的常用类 1.Math : 位于java.lang包中 (1)Math.PI:返回一个最接近圆周率的 (2)Math.abs(-10):返回一个数的绝对值 (3)Math.cbrt(27): ...

  9. java工具类 获取包下所有类

    extends:http://blog.csdn.net/jdzms23/article/details/17550119 package com.threeti.util; import java. ...

随机推荐

  1. 【已解决】【Mac】 运行adb提示command not found,需要配置adb环境

    问题:运行adb提示command not found  解决措施: 1.下载安装:android-sdk-macosx 下载路径:http://down.tech.sina.com.cn/page/ ...

  2. 为什么说 Java 程序员到了必须掌握 Spring Boot 的时候?

    Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,就单从我个人的博客的访问量大幅增加就可以感受到大家对学习 Spring Boot 的热情,那么在这么多人热衷于学习 ...

  3. 华为云(ECS)-linux服务器中-Ubuntu图形界面安装-解决root登录受限-VNCviwer/Teamviwer远程访问教程

    安装ubuntu-desktop .更新软件库 apt-get update .升级软件 apt-get upgrade .安装桌面 apt-get install ubuntu-desktop 解决 ...

  4. Exp6 信息搜集与漏洞扫描 20164312 马孝涛

    1.实践内容 (1)各种搜索技巧的应用  (2)DNS IP注册信息的查询  (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点(以自己主机为目标)  (4)漏洞扫描:会扫, ...

  5. 每日分享!JavaScript的鼠标事件(11个事件)

    鼠标的11个事件 具体的事件解释如下: click:按下鼠标(通常是按下主按钮)时触发. dblclick:在同一个元素上双击鼠标时触发. mousedown:按下鼠标键时触发. mouseup:释放 ...

  6. 利用策略模式优化过多 if else 代码

    前言 不出意外,这应该是年前最后一次分享,本次来一点实际开发中会用到的小技巧. 比如平时大家是否都会写类似这样的代码: if(a){ //dosomething }else if(b){ //dosh ...

  7. 《前端之路》之 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏

    目录 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & 骨架屏 09: 前端图片 类型 & 优化 & 预加载 & 懒加载 & ...

  8. springboot中HandlerMethodArgumentResolver的使用

    springboot项目中在所有的controller方法中想增加token验证,即所有的方法都必须登陆有token之后才能访问.springboot封装了SpringMVC中的HandlerMeth ...

  9. 第14章 纪元时间转换 - IdentityModel 中文文档(v1.0.0)

    JWT令牌使用所谓的Epoch或Unix时间来表示日期/时间. IdentityModel包含用于DateTime和DateTimeOffset转换到/来自Unix时间的扩展方法: var dt = ...

  10. MVC页面扩展方法 单例模式

    MVC页面扩展方法    单例模式    /// <summary>         /// 创建一个Config内容对象         /// </summary>     ...