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. 既定値は、true です。The default is true.

次の例AttributeUsageAttributeは、 Inheritedプロパティ値がのtrue属性がInherited適用される属性とAttributeUsageAttribute 、プロパティ値がである属性の違いを示しています。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 . この例では、とInheritedAttributeNotInheritedAttributeいう2つの属性を定義します。The example defines two attributes, InheritedAttribute and NotInheritedAttribute. どちらの属性も、クラスとメソッドに適用できます。Both attributes can apply to classes and methods. AttributeUsageAttribute true Inheritedに適用された属性のプロパティはであるため、派生クラスおよび基底クラスのメソッドをオーバーライドする派生クラスのメンバーによって継承されます。InheritedAttributeBecause 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 false適用された属性のプロパティはであるため、派生クラスおよび基底クラスのメソッドをオーバーライドする派生クラスのメンバーによって継承されることはありません。 NotInheritedAttributeOn 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. 1つ目BaseAのには、というMethodA1 つのメソッドがあります。The first, BaseA, has a single method, MethodA. 2番目BaseBのには、というMethodB1 つのメソッドがあります。The second, BaseB, has a single method, MethodB. BaseAMethodAInheritedAttribute属性MethodB でタグNotInheritedAttribute付けされ、とは属性でタグ付けされます。BaseBBaseA 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 NotInheritedAttribute属性を継承DerivedBDerivedB.MethodBますが、属性を継承しません。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.)

適用対象

こちらもご覧ください