using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace YD_XinWei.Api.Utilities
{
///
/// 工具类
///
public static class Tool
{
///
/// 获取枚举的 Display.Description 值
///
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();
return displayAttr?.Description ?? string.Empty;
}
///
/// 获取枚举的 Display.Name 值(可选)
///
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();
return displayAttr?.Name ?? value.ToString();
}
}
}