using System.ComponentModel.DataAnnotations;
using System.Reflection;
using NSExt.Attributes;
namespace NSExt.Extensions;
/// 
///     EnumExtensions
/// 
public static class EnumExtensions
{
    /// 
    ///     获取显示特性
    /// 
    public static DisplayAttribute GetDisplay(this Enum me)
    {
        return me.GetAttributeOfType();
    }
    /// 
    ///     获取枚举的本地化资源描述
    /// 
    public static string ResDesc(this Enum e)
    {
        var typeOfEnum  = e.GetType();
        var typeOfField = typeOfEnum.GetField(Enum.GetName(typeOfEnum, e)!);
        var resDescAttr = typeOfField!.GetCustomAttribute>(true);
        return resDescAttr is null
            ? Enum.GetName(typeOfEnum, e)
            : typeof(T).GetProperty(resDescAttr.ResourceName)?.GetValue(default) as string;
    }
    /// 
    ///     通过类泛型类型获取特性
    /// 
    private static T GetAttributeOfType(this Enum me)
        where T : Attribute
    {
        return me.GetType().GetMember(me.ToString())[0].GetCustomAttributes(false).FirstOrDefault();
    }
}