1818 public static class Reflection
1919 {
2020 private static readonly ConcurrentDictionary < Type , ConstructorInfo > TypesWithOneConstructorCache = new ConcurrentDictionary < Type , ConstructorInfo > ( ) ;
21- private static readonly ConcurrentDictionary < Type , IEnumerable < object > > TypeAttributesCache = new ConcurrentDictionary < Type , IEnumerable < object > > ( ) ;
22- private static readonly ConcurrentDictionary < Type , IEnumerable < object > > TypeInheritedAttributesCache = new ConcurrentDictionary < Type , IEnumerable < object > > ( ) ;
21+ private static readonly ConcurrentDictionary < Type , object > TypeAttributesCache = new ConcurrentDictionary < Type , object > ( ) ;
2322 private static readonly ConcurrentDictionary < MethodInfo , IEnumerable < object > > MethodAttributesCache = new ConcurrentDictionary < MethodInfo , IEnumerable < object > > ( ) ;
2423 private static readonly ConcurrentDictionary < Type , string > FriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
2524 private static readonly ConcurrentDictionary < Type , string > FullFriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
@@ -287,9 +286,8 @@ public static T TryCreateInstance<T>(IDictionary<Type, object> constructorParame
287286 /// <returns>IEnumerable of objects representing the custom attributes.</returns>
288287 public static IEnumerable < object > GetCustomAttributes ( object obj )
289288 {
290- var type = obj . GetType ( ) ;
291- return TypeAttributesCache
292- . GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( false ) ) ;
289+ CacheComponentAttributes ( obj ) ;
290+ return TypeAttributesCache . Values ;
293291 }
294292
295293 /// <summary>
@@ -299,9 +297,8 @@ public static IEnumerable<object> GetCustomAttributes(object obj)
299297 /// <returns>IEnumerable of objects representing the custom attributes.</returns>
300298 public static IEnumerable < object > GetCustomAttributesIncludingInherited ( object obj )
301299 {
302- var type = obj . GetType ( ) ;
303- return TypeInheritedAttributesCache
304- . GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( true ) ) ;
300+ CacheComponentAttributes ( obj , true ) ;
301+ return TypeAttributesCache . Values ;
305302 }
306303
307304 public static IEnumerable < object > GetCustomAttributes ( MethodInfo method )
@@ -735,6 +732,20 @@ private static bool ObjectPropertiesAreDeeplyEqual(
735732 return result . Success ;
736733 }
737734
735+ private static void CacheComponentAttributes ( object obj , bool shouldInherit = false )
736+ {
737+ var type = obj . GetType ( ) ;
738+ var attributes = type . GetTypeInfo ( ) . GetCustomAttributes ( shouldInherit ) ;
739+ foreach ( var attribute in attributes )
740+ {
741+ var attributeType = attribute . GetType ( ) ;
742+ if ( ! TypeAttributesCache . ContainsKey ( attributeType ) )
743+ {
744+ TypeAttributesCache . TryAdd ( attributeType , attribute ) ;
745+ }
746+ }
747+ }
748+
738749 private static string GetFriendlyTypeName ( Type type , bool useFullName )
739750 {
740751 const string anonymousTypePrefix = "<>f__" ;
0 commit comments