Skip to content

Instantly share code, notes, and snippets.

@MrSmoke
Created April 24, 2020 04:25
Show Gist options
  • Save MrSmoke/6843e91981e58a192a6160973da480d4 to your computer and use it in GitHub Desktop.
Save MrSmoke/6843e91981e58a192a6160973da480d4 to your computer and use it in GitHub Desktop.
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