AttributeUsageAttribute.Inherited Property

Definition

Gets or sets a Boolean value that determines whether the indicated attribute is inherited by derived classes and overriding members.

C#
public bool Inherited { get; set; }

Property Value

true if the attribute can be inherited by derived classes and overriding members; otherwise, false. The default is true.

Examples

The following example illustrates the difference between an attribute to which an AttributeUsageAttribute attribute with an Inherited property value of true is applied and one to which AttributeUsageAttribute attribute with an Inherited property value of false is applied. The example defines two attributes, InheritedAttribute and NotInheritedAttribute. Both attributes can apply to classes and methods. Because the Inherited property of the AttributeUsageAttribute attribute applied to InheritedAttribute is true, it is inherited by derived classes and the members of derived classes that override the base class method. On the other hand, because the Inherited property of the AttributeUsageAttribute attribute applied to NotInheritedAttribute is false, it is not inherited by derived classes and the members of derived classes that override the base class method.

C#
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
                AttributeTargets.Property | AttributeTargets.Field,
                Inherited = true)]
public class InheritedAttribute : Attribute
{ }

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
                AttributeTargets.Property | AttributeTargets.Field,
                Inherited = false)]
public class NotInheritedAttribute : Attribute
{ }

The example then defines two base classes. The first, BaseA, has a single method, MethodA. The second, BaseB, has a single method, MethodB. BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedA inherits from BaseA and overrides its MethodA method. DerivedB inherits from BaseB and overrides its MethodB method.

C#
using System;
using System.Reflection;

[InheritedAttribute]
public class BaseA
{
    [InheritedAttribute]
    public virtual void MethodA()
    { }
}

public class DerivedA : BaseA
{
    public override void MethodA()
    { }
}

[NotInheritedAttribute]
public class BaseB
{
    [NotInheritedAttribute]
    public virtual void MethodB()
    { }
}

public class DerivedB : BaseB
{
    public override void MethodB()
    { }
}

public class Example
{
    public static void Main()
    {
        Type typeA = typeof(DerivedA);
        Console.WriteLine($"DerivedA has Inherited attribute: {typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}");
        MethodInfo memberA = typeA.GetMethod(nameof(DerivedA.MethodA));
        Console.WriteLine($"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}\n");

        Type typeB = typeof(DerivedB);
        Console.WriteLine($"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");
        MethodInfo memberB = typeB.GetMethod(nameof(DerivedB.MethodB));
        Console.WriteLine($"DerivedB.MemberB has NotInherited attribute: {memberB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");
    }
}
// The example displays the following output:
//       DerivedA has Inherited attribute: True
//       DerivedA.MemberA has Inherited attribute: True
//
//       DerivedB has NotInherited attribute: False
//       DerivedB.MemberB has NotInherited attribute: False

As the output from the example shows, DerivedA and DerivedA.MethodA inherit the InheritedAttribute attribute, but DerivedB and DerivedB.MethodB do not inherit the NotInheritedAttribute attribute.

Remarks

The Inherited property determines:

  • Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • Whether methods of derived classes that override a base class method tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute. (If a class inherits a base class member, it also inherits any attributes applied to that member.)

Applies to

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also