41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System.ComponentModel;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Reflection;
|
|
|
|
namespace YD_XinWei.Api.Utilities
|
|
{
|
|
/// <summary>
|
|
/// 工具类
|
|
/// </summary>
|
|
public static class Tool
|
|
{
|
|
/// <summary>
|
|
/// 获取枚举的 Display.Description 值
|
|
/// </summary>
|
|
public static string GetUnit(this Enum value)
|
|
{
|
|
if (value == null) throw new ArgumentNullException(nameof(value));
|
|
|
|
FieldInfo field = value.GetType().GetField(value.ToString());
|
|
if (field == null) return string.Empty;
|
|
|
|
var displayAttr = field.GetCustomAttribute<DisplayAttribute>();
|
|
return displayAttr?.Description ?? string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取枚举的 Display.Name 值(可选)
|
|
/// </summary>
|
|
public static string GetDisplayName(this Enum value)
|
|
{
|
|
if (value == null) throw new ArgumentNullException(nameof(value));
|
|
|
|
FieldInfo field = value.GetType().GetField(value.ToString());
|
|
if (field == null) return string.Empty;
|
|
|
|
var displayAttr = field.GetCustomAttribute<DisplayAttribute>();
|
|
return displayAttr?.Name ?? value.ToString();
|
|
}
|
|
}
|
|
}
|