@@ -133,7 +133,7 @@ public static MethodInfo GetInstanceMethod(this Type type, string methodName) =>
133
133
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134
134
public static bool HasAttribute < T > ( this MethodInfo mi ) => mi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
135
135
136
- private static Dictionary < Tuple < MemberInfo , Type > , bool > hasAttributeCache = new Dictionary < Tuple < MemberInfo , Type > , bool > ( ) ;
136
+ private static readonly ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > hasAttributeCache = new ConcurrentDictionary < Tuple < MemberInfo , Type > , bool > ( ) ;
137
137
public static bool HasAttributeCached < T > ( this MemberInfo memberInfo )
138
138
{
139
139
var key = new Tuple < MemberInfo , Type > ( memberInfo , typeof ( T ) ) ;
@@ -149,17 +149,8 @@ public static bool HasAttributeCached<T>(this MemberInfo memberInfo)
149
149
: memberInfo is MethodInfo mi
150
150
? mi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) )
151
151
: throw new NotSupportedException ( memberInfo . GetType ( ) . Name ) ;
152
-
153
- Dictionary < Tuple < MemberInfo , Type > , bool > snapshot , newCache ;
154
- do
155
- {
156
- snapshot = hasAttributeCache ;
157
- newCache = new Dictionary < Tuple < MemberInfo , Type > , bool > ( hasAttributeCache ) {
158
- [ key ] = hasAttr
159
- } ;
160
152
161
- } while ( ! ReferenceEquals (
162
- Interlocked . CompareExchange ( ref hasAttributeCache , newCache , snapshot ) , snapshot ) ) ;
153
+ hasAttributeCache [ key ] = hasAttr ;
163
154
164
155
return hasAttr ;
165
156
}
0 commit comments