22 lines
701 B
C#
22 lines
701 B
C#
using System.ComponentModel;
|
|
|
|
namespace Seyounth.Core.Extensions;
|
|
|
|
public static class EnumHelper
|
|
{
|
|
public static IEnumerable<T> GetValues<T>()
|
|
{
|
|
return Enum.GetValues(typeof(T)).Cast<T>();
|
|
}
|
|
|
|
// 获取枚举值的描述信息
|
|
public static string GetDescription<T>(this T enumValue) where T : Enum
|
|
{
|
|
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
|
|
var descriptionAttributes =
|
|
fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
|
|
return descriptionAttributes is { Length: > 0 }
|
|
? descriptionAttributes[0].Description
|
|
: enumValue.ToString();
|
|
}
|
|
} |