2025-03-28 09:49:56 +08:00

37 lines
993 B
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EShang.Common
{
/// <summary>
/// 描述特性的读取
/// </summary>
public static class DescriptionHelper
{
/// <summary>
/// 获取枚举的描述
/// </summary>
/// <param name="em"></param>
/// <returns></returns>
public static string GetEnumDescription(this Enum em)
{
Type type = em.GetType();
var name = Enum.GetName(type, em);
if (name == null)
return "";
var field = type.GetField(name);
if (field == null)
return "";
if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute))
{
return name;
}
return attribute.Description;
}
}
}