123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- namespace Bowin.Common.Utility
- {
- /// <summary>
- /// MemberInfo 的扩展
- /// </summary>
- public static class MemberInfoExtension
- {
- /// <summary>
- /// 检测 MemberInfo 对象是否具有指定的自定义特性。
- /// </summary>
- /// <typeparam name="TAttribute">自定义特性</typeparam>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <returns></returns>
- public static bool HasAttribute<TAttribute>(this MemberInfo memberInfo) where TAttribute : Attribute
- {
- //if (memberInfo == null) return false;
- return HasAttribute(memberInfo, typeof(TAttribute));
- }
- /// <summary>
- /// 检测 MemberInfo 对象是否具有指定的自定义特性。
- /// </summary>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <param name="attributeTypes">自定义特性的类型集合。</param>
- /// <returns></returns>
- public static bool HasAttribute(this MemberInfo memberInfo, params Type[] attributeTypes)
- {
- return memberInfo.GetAttribute(attributeTypes).Any();
- }
- /// <summary>
- /// 检测 MemberInfo 对象是否具有指定的自定义特性集合中的任意一项。
- /// </summary>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <param name="attributeTypes">自定义特性的类型集合。</param>
- /// <returns></returns>
- public static bool HasAnyAttribute(this MemberInfo memberInfo, params Type[] attributeTypes)
- {
- return memberInfo.GetAttribute(attributeTypes).Any();
- }
- /// <summary>
- /// 检测 MemberInfo 对象是否具有指定的自定义特性集合的所有项。
- /// </summary>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <param name="attributeTypes">自定义特性的类型集合。</param>
- /// <returns></returns>
- public static bool HasAllAttribute(this MemberInfo memberInfo, params Type[] attributeTypes)
- {
- return attributeTypes.All(attributeType => memberInfo.HasAttribute(attributeType));
- }
- /// <summary>
- /// 获取 MemberInfo 对象应用的自定义特性。
- /// </summary>
- /// <typeparam name="TAttribute">自定义特性。</typeparam>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <returns></returns>
- public static TAttribute GetAttribute<TAttribute>(this MemberInfo memberInfo) where TAttribute : Attribute
- {
- if (memberInfo == null)
- {
- return null;
- }
- return (TAttribute)GetAttribute(memberInfo, typeof(TAttribute)).FirstOrDefault();
- }
- /// <summary>
- /// 获取 MemberInfo 对象应用的自定义特性
- /// </summary>
- /// <param name="memberInfo">MemberInfo 对象的实例。</param>
- /// <param name="attributeTypes">自定义特性的类型集合。</param>
- /// <returns></returns>
- public static IEnumerable<Attribute> GetAttribute(this MemberInfo memberInfo, params Type[] attributeTypes)
- {
- if (memberInfo == null)
- {
- return Enumerable.Empty<Attribute>();
- }
- var attributes = new HashSet<object>();
- foreach (object item in attributeTypes.SelectMany(attributeType => memberInfo.GetCustomAttributes(attributeType, true)))
- {
- attributes.Add(item);
- }
- return attributes.Select(s => (Attribute)s);
- }
- }
- }
|