Created
April 24, 2020 04:25
-
-
Save MrSmoke/6843e91981e58a192a6160973da480d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AttributeCache<TAttribute> where TAttribute : Attribute | |
{ | |
private static readonly ConcurrentDictionary<MethodInfo, ReadOnlyCollection<TAttribute>> Lookup = | |
new ConcurrentDictionary<MethodInfo, ReadOnlyCollection<TAttribute>>(); | |
public static IReadOnlyCollection<TAttribute> GetOrSet(MethodInfo methodInfo) => | |
Lookup.GetOrAdd(methodInfo, CachedDelegates.GetCustomAttributes); | |
private static class CachedDelegates | |
{ | |
internal static readonly Func<ICustomAttributeProvider, ReadOnlyCollection<TAttribute>> GetCustomAttributes | |
= memberInfo => new ReadOnlyCollection<TAttribute>( | |
(TAttribute[]) memberInfo.GetCustomAttributes(typeof(TAttribute), inherit: true)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment