@@ -124,15 +124,27 @@ public static MethodInfo GetInstanceMethod(this Type type, string methodName) =>
124
124
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
125
125
public static bool HasAttribute < T > ( this Type type ) => type . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
126
126
127
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
128
+ public static bool HasAttributeOf < T > ( this Type type ) => type . AllAttributes ( ) . Any ( x => x is T ) ;
129
+
127
130
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
128
131
public static bool HasAttribute < T > ( this PropertyInfo pi ) => pi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
129
132
133
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134
+ public static bool HasAttributeOf < T > ( this PropertyInfo pi ) => pi . AllAttributes ( ) . Any ( x => x is T ) ;
135
+
130
136
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
131
137
public static bool HasAttribute < T > ( this FieldInfo fi ) => fi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
132
138
139
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
140
+ public static bool HasAttributeOf < T > ( this FieldInfo fi ) => fi . AllAttributes ( ) . Any ( x => x is T ) ;
141
+
133
142
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134
143
public static bool HasAttribute < T > ( this MethodInfo mi ) => mi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
135
144
145
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
146
+ public static bool HasAttributeOf < T > ( this MethodInfo mi ) => mi . AllAttributes ( ) . Any ( x => x is T ) ;
147
+
136
148
private static readonly ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > hasAttributeCache = new ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > ( ) ;
137
149
public static bool HasAttributeCached < T > ( this MemberInfo memberInfo )
138
150
{
@@ -155,6 +167,28 @@ public static bool HasAttributeCached<T>(this MemberInfo memberInfo)
155
167
return hasAttr ;
156
168
}
157
169
170
+ private static readonly ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > hasAttributeOfCache = new ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > ( ) ;
171
+ public static bool HasAttributeOfCached < T > ( this MemberInfo memberInfo )
172
+ {
173
+ var key = new Tuple < MemberInfo , Type > ( memberInfo , typeof ( T ) ) ;
174
+ if ( hasAttributeOfCache . TryGetValue ( key , out var hasAttr ) )
175
+ return hasAttr ;
176
+
177
+ hasAttr = memberInfo is Type t
178
+ ? t . AllAttributes ( ) . Any ( x => x is T )
179
+ : memberInfo is PropertyInfo pi
180
+ ? pi . AllAttributes ( ) . Any ( x => x is T )
181
+ : memberInfo is FieldInfo fi
182
+ ? fi . AllAttributes ( ) . Any ( x => x is T )
183
+ : memberInfo is MethodInfo mi
184
+ ? mi . AllAttributes ( ) . Any ( x => x is T )
185
+ : throw new NotSupportedException ( memberInfo . GetType ( ) . Name ) ;
186
+
187
+ hasAttributeOfCache [ key ] = hasAttr ;
188
+
189
+ return hasAttr ;
190
+ }
191
+
158
192
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
159
193
public static bool HasAttributeNamed ( this Type type , string name )
160
194
{
0 commit comments