2025-03-16 03:17:36 +08:00

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();
}
}