Compartilhar via


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.

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

Boolean

true se o atributo pode ser herdado por classes derivadas e pela substituição de membros; caso contrário, false. O padrão é true.

Exemplos

O exemplo a seguir ilustra a diferença entre um atributo ao qual um AttributeUsageAttribute atributo com um Inherited valor de propriedade true é aplicado e outro ao qual o AttributeUsageAttribute atributo com um Inherited valor de propriedade false é aplicado. O exemplo define dois atributos InheritedAttribute e NotInheritedAttribute . Ambos os atributos podem ser aplicados a classes e métodos. Como a Inherited Propriedade do AttributeUsageAttribute atributo aplicado InheritedAttribute é true , ela é herdada por classes derivadas e os membros de classes derivadas que substituem o método da classe base. Por outro lado, como a Inherited Propriedade do AttributeUsageAttribute atributo aplicada a NotInheritedAttribute é false , ela não é herdada por classes derivadas e os membros de classes derivadas que substituem o método de classe base.

[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 ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field, Inherited = true)>]
type InheritedAttribute() =
    inherit Attribute()

[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field, Inherited = false)>]
type NotInheritedAttribute() =
    inherit 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. O primeiro, BaseA , tem um único método, MethodA . O segundo, BaseB , tem um único método, MethodB . BaseA e MethodA são marcados com o InheritedAttribute atributo e BaseB e MethodB são marcados com o NotInheritedAttribute atributo. DerivedA herda de BaseA e substitui seu MethodA método. DerivedB herda de BaseB e substitui seu MethodB método.

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
[<Inherited>]
type BaseA() =
    abstract member MethodA: unit -> unit
    [<Inherited>]
    default _.MethodA() = ()

type DerivedA() =
    inherit BaseA()

    override _.MethodA() = ()

[<NotInherited>]
type BaseB() =
    abstract member MethodB: unit -> unit
    [<NotInherited>]
    default _.MethodB() = ()

type DerivedB() =
    inherit BaseB()

    override _.MethodB() = ()

let typeA = typeof<DerivedA>
printfn $"DerivedA has Inherited attribute: {typeA.GetCustomAttributes(typeof<InheritedAttribute>, true).Length > 0}"
let memberA = typeA.GetMethod "MethodA"
printfn $"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(typeof<InheritedAttribute>, true).Length > 0}\n"

let typeB = typeof<DerivedB>
printfn $"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(typeof<NotInheritedAttribute>, true).Length > 0}"
let memberB = typeB.GetMethod "MethodB"
printfn $"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 herda o InheritedAttribute atributo, mas DerivedB DerivedB.MethodB não herda o NotInheritedAttribute atributo.

Comentários

A Inherited propriedade determina:

  • Se as classes derivadas de uma classe base marcada com o atributo ao qual o AttributeUsageAttribute atributo é aplicado herdam esse atributo.

  • Se os métodos de classes derivadas que substituem um método de classe base marcado com o atributo ao qual o AttributeUsageAttribute atributo é aplicado herdam esse atributo. (Se uma classe herdar um membro de classe base, ela também herdará todos os atributos aplicados a esse membro.)

Aplica-se a

Confira também