AttributeUsageAttribute.Inherited Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera lub ustawia Boolean wartość, która określa, czy wskazany atrybut jest dziedziczony przez klasy pochodne i zastępowanie składowych.
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
Wartość właściwości
true jeśli atrybut może być dziedziczony przez klasy pochodne i zastępowanie składowych; w przeciwnym razie, false. Wartość domyślna to true.
Przykłady
W poniższym przykładzie pokazano różnicę między atrybutem, do którego AttributeUsageAttribute zastosowano atrybut z wartością Inheritedtrue właściwości, a jednym z atrybutów AttributeUsageAttributefalse z wartością Inherited właściwości. W przykładzie zdefiniowano dwa atrybuty i InheritedAttributeNotInheritedAttribute. Oba atrybuty mogą być stosowane do klas i metod.
Inherited Ponieważ właściwość atrybutu zastosowanego AttributeUsageAttribute do InheritedAttribute elementu to true, jest dziedziczona przez klasy pochodne i składowe klas pochodnych, które zastępują metodę klasy bazowej. Z drugiej strony, ponieważ Inherited właściwość atrybutu zastosowanego AttributeUsageAttribute do NotInheritedAttribute elementu to false, nie jest dziedziczona przez klasy pochodne i składowe klas pochodnych, które zastępują metodę klasy bazowej.
[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
W tym przykładzie zdefiniowano dwie klasy bazowe. Pierwsza, , BaseAma jedną metodę , MethodA. Drugi element , BaseBma jedną metodę . MethodB
BaseA i MethodA są oznaczone atrybutem InheritedAttribute i BaseBMethodB są oznaczone atrybutem NotInheritedAttribute .
DerivedA dziedziczy metodę BaseA i zastępuje ją MethodA .
DerivedB dziedziczy metodę BaseB i zastępuje ją MethodB .
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
Jak pokazuje dane wyjściowe z przykładu i DerivedADerivedA.MethodA dziedziczą atrybut, ale DerivedB i DerivedB.MethodB nie dziedziczą InheritedAttribute atrybutuNotInheritedAttribute.
Uwagi
Właściwość Inherited określa:
Określa, czy klasy pochodzące z klasy bazowej oznaczone atrybutem, do którego AttributeUsageAttribute zastosowano atrybut, dziedziczą ten atrybut.
Określa, czy metody klas pochodnych, które zastępują metodę klasy bazowej oznaczone atrybutem, do którego AttributeUsageAttribute zastosowano atrybut dziedziczą ten atrybut. (Jeśli klasa dziedziczy składową klasy bazowej, dziedziczy również wszystkie atrybuty zastosowane do tego elementu członkowskiego).