AttributeUsageAttribute.Inherited Propriedade

Definição

Obtém ou define um valor Boolean que determina se o atributo indicado é herdado por classes derivadas e pela substituição de membros.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 da propriedade

true se o atributo pode ser herdado por classes derivadas e pela substituição de membros; caso contrário, false.true if the attribute can be inherited by derived classes and overriding members; otherwise, false. O padrão é true.The default is true.

Exemplos

O exemplo a seguir ilustra a diferença entre um atributo para o qual um atributo AttributeUsageAttribute com um valor de propriedade Inherited de true é aplicado e outro ao qual AttributeUsageAttribute atributo com um valor de propriedade Inherited de false é aplicado.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 . O exemplo define dois atributos, InheritedAttribute e NotInheritedAttribute.The example defines two attributes, InheritedAttribute and NotInheritedAttribute. Ambos os atributos podem ser aplicados a classes e métodos.Both attributes can apply to classes and methods. Como a propriedade Inherited do atributo AttributeUsageAttribute aplicado a InheritedAttribute é true, ela é herdada por classes derivadas e os membros de classes derivadas que substituem o método da classe 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 outro lado, como a propriedade Inherited do atributo AttributeUsageAttribute aplicado a NotInheritedAttribute é false, ela não é herdada por classes derivadas e os membros de classes derivadas que substituem o método de classe base.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

Em seguida, o exemplo define duas classes base.The example then defines two base classes. O primeiro, BaseA, tem um único método, MethodA.The first, BaseA, has a single method, MethodA. O segundo, BaseB, tem um único método, MethodB.The second, BaseB, has a single method, MethodB. BaseA e MethodA são marcados com o atributo InheritedAttribute e BaseB e MethodB são marcados com o atributo NotInheritedAttribute.BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedA herda de BaseA e substitui seu método MethodA.DerivedA inherits from BaseA and overrides its MethodA method. DerivedB herda de BaseB e substitui seu método MethodB.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 a saída do exemplo mostra, DerivedA e DerivedA.MethodA herdam o atributo InheritedAttribute, mas DerivedB e DerivedB.MethodB não herdam o atributo NotInheritedAttribute.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.

Comentários

A propriedade Inherited determina:The Inherited property determines:

  • Se as classes derivadas de uma classe base marcada com o atributo ao qual o atributo AttributeUsageAttribute é aplicado herdam esse atributo.Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • Se os métodos de classes derivadas que substituem um método de classe base marcado com o atributo ao qual o atributo AttributeUsageAttribute é aplicado herdam esse 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. (Se uma classe herdar um membro de classe base, ela também herdará todos os atributos aplicados a esse membro.)(If a class inherits a base class member, it also inherits any attributes applied to that member.)

Aplica-se a

Veja também