AttributeUsageAttribute.Inherited 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 Boolean 值,判斷指示的屬性是否由衍伸類別繼承並覆寫成員。
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
屬性值
如果屬性可以由衍伸類別繼承並覆寫成員,則為 true
;否則為 false
。 預設為 true
。
範例
下列範例說明 AttributeUsageAttribute Inherited 套用屬性值 true
的屬性,以及 false
AttributeUsageAttribute Inherited 套用屬性值的屬性之間的差異。 此範例會定義兩個屬性和 InheritedAttribute
NotInheritedAttribute
。 這兩個屬性都可以套用至類別和方法。 Inherited因為套用 InheritedAttribute
屬性的 AttributeUsageAttribute 屬性是 true
,所以會由衍生類別和覆寫基類方法之衍生類別的成員繼承。 另一方面,因為 Inherited 套用 NotInheritedAttribute
至 之屬性的 AttributeUsageAttribute 屬性是 false
,所以不會由衍生類別和覆寫基類方法之衍生類別的成員繼承。
[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
此範例接著會定義兩個基類。 第一個 是 BaseA
,具有單一方法 MethodA
。 第二個 是 BaseB
,具有單一方法 MethodB
。 BaseA
和 MethodA
會以 InheritedAttribute
屬性標記,並以 BaseB
MethodB
屬性標記 NotInheritedAttribute
。 DerivedA
繼承自 BaseA
並覆寫其 MethodA
方法。 DerivedB
繼承自 BaseB
並覆寫其 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
如範例的輸出所示, DerivedA
並 DerivedA.MethodA
繼承 InheritedAttribute
屬性,但 DerivedB
DerivedB.MethodB
不會繼承 NotInheritedAttribute
屬性。
備註
屬性 Inherited 決定:
衍生自基類的類別是否使用套用屬性 AttributeUsageAttribute 所標記的基類繼承該屬性。
套用屬性之屬性 AttributeUsageAttribute 所標記之基類方法的衍生類別方法是否繼承該屬性。 (如果類別繼承基類成員,它也會繼承套用至該成員的任何屬性。)