18
18
public static class Reflection
19
19
{
20
20
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 > ( ) ;
23
22
private static readonly ConcurrentDictionary < MethodInfo , IEnumerable < object > > MethodAttributesCache = new ConcurrentDictionary < MethodInfo , IEnumerable < object > > ( ) ;
24
23
private static readonly ConcurrentDictionary < Type , string > FriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
25
24
private static readonly ConcurrentDictionary < Type , string > FullFriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
@@ -287,9 +286,8 @@ public static T TryCreateInstance<T>(IDictionary<Type, object> constructorParame
287
286
/// <returns>IEnumerable of objects representing the custom attributes.</returns>
288
287
public static IEnumerable < object > GetCustomAttributes ( object obj )
289
288
{
290
- var type = obj . GetType ( ) ;
291
- return TypeAttributesCache
292
- . GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( false ) ) ;
289
+ CacheComponentAttributes ( obj ) ;
290
+ return TypeAttributesCache . Values ;
293
291
}
294
292
295
293
/// <summary>
@@ -299,9 +297,8 @@ public static IEnumerable<object> GetCustomAttributes(object obj)
299
297
/// <returns>IEnumerable of objects representing the custom attributes.</returns>
300
298
public static IEnumerable < object > GetCustomAttributesIncludingInherited ( object obj )
301
299
{
302
- var type = obj . GetType ( ) ;
303
- return TypeInheritedAttributesCache
304
- . GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( true ) ) ;
300
+ CacheComponentAttributes ( obj , true ) ;
301
+ return TypeAttributesCache . Values ;
305
302
}
306
303
307
304
public static IEnumerable < object > GetCustomAttributes ( MethodInfo method )
@@ -735,6 +732,20 @@ private static bool ObjectPropertiesAreDeeplyEqual(
735
732
return result . Success ;
736
733
}
737
734
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
+
738
749
private static string GetFriendlyTypeName ( Type type , bool useFullName )
739
750
{
740
751
const string anonymousTypePrefix = "<>f__" ;
0 commit comments