@@ -19,6 +19,7 @@ public static class Reflection
19
19
{
20
20
private static readonly ConcurrentDictionary < Type , ConstructorInfo > TypesWithOneConstructorCache = new ConcurrentDictionary < Type , ConstructorInfo > ( ) ;
21
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 > > ( ) ;
22
23
private static readonly ConcurrentDictionary < MethodInfo , IEnumerable < object > > MethodAttributesCache = new ConcurrentDictionary < MethodInfo , IEnumerable < object > > ( ) ;
23
24
private static readonly ConcurrentDictionary < Type , string > FriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
24
25
private static readonly ConcurrentDictionary < Type , string > FullFriendlyTypeNames = new ConcurrentDictionary < Type , string > ( ) ;
@@ -291,6 +292,18 @@ public static IEnumerable<object> GetCustomAttributes(object obj)
291
292
. GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( false ) ) ;
292
293
}
293
294
295
+ /// <summary>
296
+ /// Gets custom attributes including inherited ones on the provided object.
297
+ /// </summary>
298
+ /// <param name="obj">Object decorated with custom attribute.</param>
299
+ /// <returns>IEnumerable of objects representing the custom attributes.</returns>
300
+ public static IEnumerable < object > GetCustomAttributesIncludingInherited ( object obj )
301
+ {
302
+ var type = obj . GetType ( ) ;
303
+ return TypeInheritedAttributesCache
304
+ . GetOrAdd ( type , _ => type . GetTypeInfo ( ) . GetCustomAttributes ( true ) ) ;
305
+ }
306
+
294
307
public static IEnumerable < object > GetCustomAttributes ( MethodInfo method )
295
308
=> MethodAttributesCache
296
309
. GetOrAdd ( method , _ => method . GetCustomAttributes ( false ) ) ;
0 commit comments