AttributeUsageAttribute.Inherited Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri vagy beállít egy Boolean értéket, amely meghatározza, hogy a megadott attribútumot származtatott osztályok és felülíró tagok öröklik-e.
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
Tulajdonság értéke
trueha az attribútumot származtatott osztályok és felülküldő tagok örökölhetik; egyéb esetben. false Az alapértelmezett érték a true.
Példák
Az alábbi példa egy olyan attribútum különbségét szemlélteti, amelyre egy tulajdonságértékkel AttributeUsageAttribute rendelkező Inherited attribútumot alkalmaz, és egy olyan attribútumottrue, amelyre AttributeUsageAttribute a tulajdonságérték Inherited vonatkozik.false A példa két attribútumot InheritedAttributeNotInheritedAttributeés . Mindkét attribútum alkalmazható osztályokra és metódusokra. Mivel az InheritedAttributeUsageAttribute alkalmazott InheritedAttribute attribútum tulajdonsága az true, a származtatott osztályok és a származtatott osztályok azon tagjai öröklik, amelyek felülbírálják az alaposztály-metódust. Másrészt, mivel az Inherited alkalmazott AttributeUsageAttributeNotInheritedAttributeattribútum tulajdonsága false az, nem örökli a származtatott osztályok és a származtatott osztályok tagjai, amelyek felülírják az alaposztály-metódust.
[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
A példa ezután két alaposztályt határoz meg. Az első, BaseAegyetlen metódussal rendelkezik. MethodA A második, BaseBegyetlen metódussal rendelkezik. MethodB
BaseA és MethodA címkézve vannak az InheritedAttribute attribútummal, és BaseBMethodB címkézve vannak az NotInheritedAttribute attribútummal.
DerivedA
BaseA örökli és felülbírálja a metódusátMethodA.
DerivedB
BaseB örökli és felülbírálja a metódusátMethodB.
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
Ahogy a példa kimenete mutatja, DerivedA és DerivedA.MethodA örökli az InheritedAttribute attribútumot, de DerivedBDerivedB.MethodB nem örökli az NotInheritedAttribute attribútumot.
Megjegyzések
A Inherited tulajdonság a következőt határozza meg:
Azt határozza meg, hogy az attribútumot alkalmazó attribútummal címkézett alaposztályból származtatott osztályok öröklik-e ezt az AttributeUsageAttribute attribútumot.
Azt határozza meg, hogy a származtatott osztályok azon metódusai, amelyek felülbírálnak egy alaposztály-metódust azzal az attribútummal címkézve, amelyre az attribútumot alkalmazzák, öröklik-e ezt az AttributeUsageAttribute attribútumot. (Ha egy osztály örököl egy alaposztálytagot, az az adott tagra alkalmazott attribútumokat is örökli.)