If you apply an attribute that has Inherited = true
set in its AttributeUsage
attribute, and then call GetCustomAttributes(inherit: true)
on a member that inherits from a member with that attribute, then you're going to get that attribute. But you're not going to see anything in the IL of the inheriting member, the compiler doesn't do anything special for it, it's reflection that looks at the base member.
For example with this code:
[AttributeUsage(AttributeTargets.All, Inherited = true)]
class InheritedAttribute : Attribute
{}
[AttributeUsage(AttributeTargets.All, Inherited = false)]
class NotInheritedAttribute : Attribute
{}
abstract class Base
{
[Inherited, NotInherited]
public abstract void M();
}
class Derived : Base
{
public override void M()
{}
}
…
foreach (var type in new[] { typeof(Base), typeof(Derived) })
{
var method = type.GetMethod("M");
foreach (var inherit in new[] { true, false })
{
var attributes = method.GetCustomAttributes(inherit);
Console.WriteLine(
"{0}.{1}, inherit={2}: {3}",
method.ReflectedType.Name, method.Name, inherit,
string.Join(", ", attributes.Select(a => a.GetType().Name)));
}
}
You're going to get this output:
Base.M, inherit=True: NotInheritedAttribute, InheritedAttribute
Base.M, inherit=False: NotInheritedAttribute, InheritedAttribute
Derived.M, inherit=True: InheritedAttribute
Derived.M, inherit=False: