Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 628a4a1

Browse files
committed
Update PlatformExtensions.cs
1 parent 43f4c1d commit 628a4a1

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)