Если применить атрибут, который имеет Inherited = true
набор в атрибуте AttributeUsage
, а затем вызвать GetCustomAttributes(inherit: true)
на член, который наследуется от элемента с этим атрибутом, то вы собираетесь получить этот атрибут. Но вы не увидите ничего в IL наследующего члена, компилятор не делает ничего особенного для него, это отражение, которое смотрит на базовый элемент.
Например, с этим кодом:
[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)));
}
}
Вы собираетесь получить этот результат:
Base.M, inherit=True: NotInheritedAttribute, InheritedAttribute
Base.M, inherit=False: NotInheritedAttribute, InheritedAttribute
Derived.M, inherit=True: InheritedAttribute
Derived.M, inherit=False:
Атрибуты никогда не унаследованного, если вы не учите его –
насчет атрибута TestFixtureTeardown из NUnit 2.6. Икс? –
В документации NUnit сказано: «Атрибут TestFixtureTearDown унаследован от любого базового класса». –