@@ -133,7 +133,7 @@ public static MethodInfo GetInstanceMethod(this Type type, string methodName) =>
133133 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
134134 public static bool HasAttribute < T > ( this MethodInfo mi ) => mi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) ) ;
135135
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 > ( ) ;
137137 public static bool HasAttributeCached < T > ( this MemberInfo memberInfo )
138138 {
139139 var key = new Tuple < MemberInfo , Type > ( memberInfo , typeof ( T ) ) ;
@@ -149,17 +149,8 @@ public static bool HasAttributeCached<T>(this MemberInfo memberInfo)
149149 : memberInfo is MethodInfo mi
150150 ? mi . AllAttributes ( ) . Any ( x => x . GetType ( ) == typeof ( T ) )
151151 : 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- } ;
160152
161- } while ( ! ReferenceEquals (
162- Interlocked . CompareExchange ( ref hasAttributeCache , newCache , snapshot ) , snapshot ) ) ;
153+ hasAttributeCache [ key ] = hasAttr ;
163154
164155 return hasAttr ;
165156 }
0 commit comments