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

Commit bb5e79e

Browse files
committed
Add HasAttributeOf* APIs
1 parent 80e39fe commit bb5e79e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,27 @@ public static MethodInfo GetInstanceMethod(this Type type, string methodName) =>
124124
[MethodImpl(MethodImplOptions.AggressiveInlining)]
125125
public static bool HasAttribute<T>(this Type type) => type.AllAttributes().Any(x => x.GetType() == typeof(T));
126126

127+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128+
public static bool HasAttributeOf<T>(this Type type) => type.AllAttributes().Any(x => x is T);
129+
127130
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128131
public static bool HasAttribute<T>(this PropertyInfo pi) => pi.AllAttributes().Any(x => x.GetType() == typeof(T));
129132

133+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
134+
public static bool HasAttributeOf<T>(this PropertyInfo pi) => pi.AllAttributes().Any(x => x is T);
135+
130136
[MethodImpl(MethodImplOptions.AggressiveInlining)]
131137
public static bool HasAttribute<T>(this FieldInfo fi) => fi.AllAttributes().Any(x => x.GetType() == typeof(T));
132138

139+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
140+
public static bool HasAttributeOf<T>(this FieldInfo fi) => fi.AllAttributes().Any(x => x is T);
141+
133142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
134143
public static bool HasAttribute<T>(this MethodInfo mi) => mi.AllAttributes().Any(x => x.GetType() == typeof(T));
135144

145+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
146+
public static bool HasAttributeOf<T>(this MethodInfo mi) => mi.AllAttributes().Any(x => x is T);
147+
136148
private static readonly ConcurrentDictionary<Tuple<MemberInfo,Type>, bool> hasAttributeCache = new ConcurrentDictionary<Tuple<MemberInfo,Type>, bool>();
137149
public static bool HasAttributeCached<T>(this MemberInfo memberInfo)
138150
{
@@ -155,6 +167,28 @@ public static bool HasAttributeCached<T>(this MemberInfo memberInfo)
155167
return hasAttr;
156168
}
157169

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+
158192
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159193
public static bool HasAttributeNamed(this Type type, string name)
160194
{

0 commit comments

Comments
 (0)