AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited Property

Definición

Obtiene o establece un valor Boolean que determina si el atributo indicado se hereda por las clases derivadas y por los miembros de invalidación.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

Valor de propiedad

Es true si el atributo lo pueden heredar clases derivadas y miembros de invalidación; de lo contrario, false.true if the attribute can be inherited by derived classes and overriding members; otherwise, false. De manera predeterminada, es true.The default is true.

Ejemplos

En el ejemplo siguiente se muestra la diferencia entre un atributo al que AttributeUsageAttribute se aplica un Inherited atributo con un true valor de propiedad de y un AttributeUsageAttribute atributo al que Inherited tiene un valor de propiedad de. false se aplica.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 . En el ejemplo se definen dos InheritedAttribute atributos NotInheritedAttribute, y.The example defines two attributes, InheritedAttribute and NotInheritedAttribute. Ambos atributos pueden aplicarse a clases y métodos.Both attributes can apply to classes and methods. Dado que Inherited la propiedad AttributeUsageAttribute del atributo aplicado a InheritedAttribute es true, la heredan las clases derivadas y los miembros de las clases derivadas que invalidan el método de la clase base.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. Por otro lado, dado que la Inherited propiedad AttributeUsageAttribute del atributo que se aplica NotInheritedAttribute a falsees, las clases derivadas y los miembros de las clases derivadas que invalidan el método de clase base no lo heredan.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

A continuación, en el ejemplo se definen dos clases base.The example then defines two base classes. La primera, BaseA, tiene un único método, MethodA.The first, BaseA, has a single method, MethodA. El segundo, BaseB, tiene un único método, MethodB.The second, BaseB, has a single method, MethodB. BaseAy MethodA se etiquetan con InheritedAttribute el atributo BaseB y y MethodB se etiquetan con NotInheritedAttribute el atributo.BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedAhereda de BaseA e invalida su MethodA método.DerivedA inherits from BaseA and overrides its MethodA method. DerivedBhereda de BaseB e invalida su MethodB método.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

Como muestra la salida del ejemplo, DerivedA y DerivedA.MethodA hereda el InheritedAttribute atributo, pero DerivedB y DerivedB.MethodB no heredan el NotInheritedAttribute atributo.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.

Comentarios

La Inherited propiedad determina:The Inherited property determines:

  • Si las clases derivadas de una clase base etiquetada con el atributo AttributeUsageAttribute al que se aplica el atributo heredan ese atributo.Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • Si los métodos de clases derivadas que reemplazan un método de clase base etiquetado con el AttributeUsageAttribute atributo al que se aplica el atributo heredan ese atributo.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. (Si una clase hereda un miembro de clase base, también hereda cualquier atributo aplicado a ese miembro).(If a class inherits a base class member, it also inherits any attributes applied to that member.)

Se aplica a

Consulte también: