.net工具类 获取枚举类型的描述

时间:2023-03-09 14:58:13
.net工具类 获取枚举类型的描述

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

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

    /// <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();

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