using System.Reflection;

namespace Hotline.Share.Tools;
public static class TypeExtensions
{
    /// <summary>
    /// 检查指定指定类型成员中是否存在指定的Attribute特性
    /// </summary>
    /// <typeparam name="T">要检查的Attribute特性类型</typeparam>
    /// <param name="memberInfo">要检查的类型成员</param>
    /// <param name="inherit">是否从继承中查找</param>
    /// <returns>是否存在</returns>
    public static bool HasAttribute<T>(this MemberInfo memberInfo, bool inherit = false) where T : Attribute
    {
        return memberInfo.IsDefined(typeof(T), inherit);
    }

    /// <summary>
    /// 从类型成员获取指定Attribute特性
    /// </summary>
    /// <typeparam name="T">Attribute特性类型</typeparam>
    /// <param name="memberInfo">类型类型成员</param>
    /// <param name="inherit">是否从继承中查找</param>
    /// <returns>存在返回第一个，不存在返回null</returns>
    public static T GetAttribute<T>(this MemberInfo memberInfo, bool inherit = false) where T : Attribute
    {
        var descripts = memberInfo.GetCustomAttributes(typeof(T), inherit);
        return descripts.FirstOrDefault() as T;
    }
}
