using System;
using System.Linq;
using System.Reflection;
namespace Abp.Reflection.Extensions
{
///
/// Extensions to .
///
public static class MemberInfoExtensions
{
///
/// Gets a single attribute for a member.
///
/// Type of the attribute
/// The member that will be checked for the attribute
/// Include inherited attributes
/// Returns the attribute object if found. Returns null if not found.
public static TAttribute GetSingleAttributeOrNull(this MemberInfo memberInfo, bool inherit = true)
where TAttribute : Attribute
{
if (memberInfo == null)
{
throw new ArgumentNullException(nameof(memberInfo));
}
var attrs = memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).ToArray();
if (attrs.Length > 0)
{
return (TAttribute)attrs[0];
}
return default(TAttribute);
}
public static TAttribute GetSingleAttributeOfTypeOrBaseTypesOrNull(this Type type, bool inherit = true)
where TAttribute : Attribute
{
var attr = type.GetTypeInfo().GetSingleAttributeOrNull();
if (attr != null)
{
return attr;
}
if (type.GetTypeInfo().BaseType == null)
{
return null;
}
return type.GetTypeInfo().BaseType.GetSingleAttributeOfTypeOrBaseTypesOrNull(inherit);
}
}
}