AttributeUsageAttribute.Inherited 屬性

定義

取得或設定 Boolean 值,判斷指示的屬性是否由衍伸類別繼承並覆寫成員。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

屬性值

如果屬性可以由衍伸類別繼承並覆寫成員,則為 true;否則為 falsetrue if the attribute can be inherited by derived classes and overriding members; otherwise, false. 預設為 trueThe default is true.

範例

下列範例說明套用了具有 Inherited 屬性值 true 的屬性 AttributeUsageAttribute (attribute),以及套用 Inherited 屬性值為 falseAttributeUsageAttribute 屬性(property)之間的差異。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 . 此範例會定義兩個屬性,InheritedAttributeNotInheritedAttributeThe example defines two attributes, InheritedAttribute and NotInheritedAttribute. 這兩個屬性都可以套用至類別和方法。Both attributes can apply to classes and methods. 因為套用至 InheritedAttributeAttributeUsageAttribute 屬性的 Inherited 屬性是 true,所以衍生類別和覆寫基類方法的衍生類別成員會繼承它。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. 另一方面,因為套用至 NotInheritedAttributeAttributeUsageAttribute 屬性的 Inherited 屬性是 false,所以衍生類別和覆寫基類方法的衍生類別成員不會繼承它。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

然後,此範例會定義兩個基類。The example then defines two base classes. 第一個(BaseA)具有單一方法,MethodAThe first, BaseA, has a single method, MethodA. 第二個(BaseB)有單一方法,MethodBThe second, BaseB, has a single method, MethodB. BaseAMethodA 會加上 InheritedAttribute 屬性的標籤,而且 BaseBMethodB 會加上 NotInheritedAttribute 屬性的標記。BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedA 繼承自 BaseA,並覆寫其 MethodA 方法。DerivedA inherits from BaseA and overrides its MethodA method. DerivedB 繼承自 BaseB,並覆寫其 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

如範例的輸出所示,DerivedADerivedA.MethodA 會繼承 InheritedAttribute 屬性,但 DerivedBDerivedB.MethodB 不會繼承 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.

備註

Inherited 屬性會決定:The Inherited property determines:

  • 如果衍生自基類的類別是以套用 AttributeUsageAttribute 屬性的屬性(attribute)來標記,則會繼承該屬性。Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • 如果衍生類別的方法會覆寫以套用 AttributeUsageAttribute 屬性之屬性(attribute)標記的基類方法,會繼承該屬性(attribute)。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. (如果類別繼承基類成員,它也會繼承套用至該成員的任何屬性)。(If a class inherits a base class member, it also inherits any attributes applied to that member.)

適用於

另請參閱