Falcon.SugarApi/Falcon.SugarApi/TypeExtend.cs

38 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Falcon.SugarApi
{
/// <summary>
/// 类型相关扩展
/// </summary>
public static class TypeExtend
{
/// <summary>
/// 尝试获取Attribute
/// </summary>
/// <typeparam name="T">Attribute类型</typeparam>
/// <param name="info">属性</param>
/// <param name="p">定义的特性</param>
/// <returns>定义返回True否则False</returns>
public static bool TryGetAttribute<T>([NotNull] this PropertyInfo info, out T p) where T : Attribute {
p = info.GetCustomAttribute<T>();
return p != null;
}
/// <summary>
/// 尝试获取Attribute
/// </summary>
/// <typeparam name="T">Attribute类型</typeparam>
/// <param name="info">属性</param>
/// <param name="p">定义的特性</param>
/// <returns>定义返回True否则False</returns>
public static bool TryGetAttribute<T>([NotNull] this Type info, out T p) where T : Attribute {
p = info.GetCustomAttribute<T>();
return p != null;
}
}
}