AttributeUsageAttribute.Inherited Eigenschaft

Definition

Ruft einen Boolean-Wert ab, der bestimmt, ob das angegebene Attribut von abgeleiteten Klassen und überschreibenden Membern geerbt wird, oder legt diesen Wert fest.Gets or sets a Boolean value that determines whether the indicated attribute is inherited by derived classes and overriding members.

public:
 property bool Inherited { bool get(); void set(bool value); };
public bool Inherited { get; set; }
member this.Inherited : bool with get, set
Public Property Inherited As Boolean

Eigenschaftswert

true, wenn das Attribut von abgeleiteten Klassen und überschreibenden Membern geerbt werden kann, andernfalls false.true if the attribute can be inherited by derived classes and overriding members; otherwise, false. Der Standardwert ist true.The default is true.

Beispiele

Das folgende Beispiel veranschaulicht den Unterschied zwischen einem Attribut, mit dem ein AttributeUsageAttribute Attribut mit einem Inherited-Eigenschafts Wert true angewendet wird, und einer, auf die AttributeUsageAttribute Attribut mit einem Inherited-Eigenschafts Wert false angewendet wird.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 . Im Beispiel werden zwei Attribute definiert, InheritedAttribute und NotInheritedAttribute.The example defines two attributes, InheritedAttribute and NotInheritedAttribute. Beide Attribute können auf Klassen und Methoden angewendet werden.Both attributes can apply to classes and methods. Da die Inherited-Eigenschaft des auf InheritedAttribute angewendeten AttributeUsageAttribute Attributs trueist, wird Sie von abgeleiteten Klassen und den Membern abgeleiteter Klassen geerbt, die die Basisklassen Methode überschreiben.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. Da die Inherited-Eigenschaft des auf NotInheritedAttribute angewendeten AttributeUsageAttribute Attributs falseist, wird Sie jedoch nicht von abgeleiteten Klassen und den Membern abgeleiteter Klassen geerbt, die die Basisklassen Methode überschreiben.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.

[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
{ }
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method _
                Or AttributeTargets.Property Or AttributeTargets.Field,
                Inherited:=True)>
Public Class InheritedAttribute : Inherits Attribute
End Class

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method _
                Or AttributeTargets.Property Or AttributeTargets.Field,
                Inherited:=False)>
Public Class NotInheritedAttribute : Inherits Attribute
End Class

Im Beispiel werden dann zwei Basisklassen definiert.The example then defines two base classes. Der erste, BaseA, verfügt über eine einzige Methode, MethodA.The first, BaseA, has a single method, MethodA. Der zweite BaseBverfügt über eine einzige Methode, MethodB.The second, BaseB, has a single method, MethodB. BaseA und MethodA werden mit dem InheritedAttribute-Attribut markiert, und BaseB und MethodB werden mit dem NotInheritedAttribute-Attribut markiert.BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedA erbt von BaseA und überschreibt dessen MethodA-Methode.DerivedA inherits from BaseA and overrides its MethodA method. DerivedB erbt von BaseB und überschreibt dessen MethodB-Methode.DerivedB inherits from BaseB and overrides its MethodB method.

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
Imports System.Reflection

<InheritedAttribute> Public Class BaseA
    <InheritedAttribute> Public Overridable Sub MethodA()
    End Sub
End Class

Public Class DerivedA : Inherits BaseA
    Public Overrides Sub MethodA()
    End Sub
End Class

<NotInheritedAttribute> Public Class BaseB
    <NotInheritedAttribute> Public Overridable Sub MethodB()
    End Sub
End Class

Public Class DerivedB : Inherits BaseB
    Public Overrides Sub MethodB()
    End Sub
End Class

Module Example
    Public Sub Main()
        Dim typeA As Type = GetType(DerivedA)
        Console.WriteLine($"DerivedA has Inherited attribute: {typeA.GetCustomAttributes(GetType(InheritedAttribute), True).Length > 0}")
        Dim memberA As MethodInfo = typeA.GetMethod(NameOf(DerivedA.MethodA))
        Console.WriteLine($"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(GetType(InheritedAttribute), True).Length > 0}")
        Console.WriteLine()

        Dim typeB As Type = GetType(DerivedB)
        Console.WriteLine($"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(GetType(NotInheritedAttribute), True).Length > 0}")
        Dim memberB As MethodInfo = typeB.GetMethod(NameOf(DerivedB.MethodB))
        Console.WriteLine($"DerivedB.MemberB has Inherited attribute: {memberB.GetCustomAttributes(GetType(NotInheritedAttribute), True).Length > 0}")
    End Sub
End Module
' The example displays the following output:
'       DerivedA has Inherited attribute: True
'       DerivedA.MemberA has Inherited attribute: True
'       
'       DerivedB has Inherited attribute: False
'       DerivedB.MemberB has Inherited attribute: False

Wie die Ausgabe des Beispiels zeigt, DerivedA und DerivedA.MethodA das InheritedAttribute-Attribut erben, aber DerivedB und DerivedB.MethodB erben das NotInheritedAttribute-Attribut nicht.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.

Hinweise

Die Inherited-Eigenschaft bestimmt Folgendes:The Inherited property determines:

  • Gibt an, ob Klassen, die von einer Basisklasse abgeleitet werden, die mit dem-Attribut, auf das das AttributeUsageAttribute-Attribut angewendet wird, dasWhether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • Ob Methoden abgeleiteter Klassen, die eine Basisklassen Methode überschreiben, die mit dem-Attribut gekennzeichnet ist, auf das das AttributeUsageAttribute-Attribut angewendet wird, dieses Attribut erbenWhether methods of derived classes that override a base class method tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute. (Wenn eine Klasse einen Basisklassenmember erbt, erbt sie auch alle Attribute, die auf diesen Member angewendet werden.)(If a class inherits a base class member, it also inherits any attributes applied to that member.)

Gilt für:

Siehe auch