AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited AttributeUsageAttribute.Inherited Property

定義

示された属性が派生クラスによって継承され、メンバーをオーバーライドするかどうかを決定する 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. 既定値は、true です。The default is true.

次の例では、する属性間の差、AttributeUsageAttribute属性をInheritedプロパティの値trueが適用されるとする 1 つAttributeUsageAttribute属性をInheritedプロパティの値falseが適用されます。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 . 例では、2 つの属性を定義します。InheritedAttributeNotInheritedAttributeします。The example defines two attributes, InheritedAttribute and NotInheritedAttribute. 両方の属性は、クラスとメソッドに適用できます。Both attributes can apply to classes and methods. Inheritedのプロパティ、AttributeUsageAttributeに適用される属性InheritedAttributetrue、派生クラスによって継承されているのメンバーが基底クラスのメソッドをオーバーライドするクラスを派生します。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. その一方で、ため、Inheritedのプロパティ、AttributeUsageAttributeに適用される属性NotInheritedAttributefalse、派生クラスによって継承されていないのメンバーが基底クラスのメソッドをオーバーライドするクラスを派生します。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

この例は、2 つの基本クラスを定義します。The example then defines two base classes. まず、 BaseA、1 つのメソッドを持つMethodAThe first, BaseA, has a single method, MethodA. 次に、 BaseB、1 つのメソッドを持つMethodBします。The second, BaseB, has a single method, MethodB. BaseA MethodAタグが付けられた、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属性が適用される属性を継承します。Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • メソッドが基底クラスのメソッドをオーバーライドするクラスを派生するかどうかを属性でタグ付け、AttributeUsageAttribute属性が適用される属性を継承します。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.)

適用対象

こちらもご覧ください