Files
ww-jst/wmsjst/JSMachine.WMS.Infrastructure/Extensions/EnumExtension.cs
T

34 lines
938 B
C#
Raw Normal View History

2026-09-02 16:31:50 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.Infrastructure
{
public static class EnumExtension
{
/// <summary>
/// 获取枚举描述属性值
/// </summary>
/// <param name="enum"></param>
/// <returns></returns>
public static string GetDescription(this Enum @enum)
{
Type type = @enum.GetType();
string name = Enum.GetName(type, @enum);
if (string.IsNullOrWhiteSpace(name))
return @enum.ToString();
FieldInfo field = type.GetField(name);
DescriptionAttribute des = field?.GetCustomAttribute<DescriptionAttribute>();
if (des == null)
return @enum.ToString();
return des.Description;
}
}
}