AttributeUsageAttribute.Inherited プロパティ

定義

示された属性が派生クラスによって継承され、メンバーをオーバーライドするかどうかを決定する Boolean 値を取得または設定します。

C#
public bool Inherited { get; set; }

プロパティ値

Boolean

属性が派生クラスにより継承され、メンバーをオーバーライドする場合は true。それ以外の場合は false。 既定値は、true です。

次の例は、プロパティ値を持つ属性が適用されるAttributeUsageAttribute属性とInherited、プロパティ値trueが適用されるAttributeUsageAttribute属性のfalse違いをInherited示しています。 この例では、2 つの属性と NotInheritedAttribute. InheritedAttribute どちらの属性もクラスとメソッドに適用できます。 適用InheritedAttributeされるInherited属性のAttributeUsageAttributeプロパティはtrue、派生クラスと基底クラス メソッドをオーバーライドする派生クラスのメンバーによって継承されます。 一方、Inherited適用NotInheritedAttributeされる属性のAttributeUsageAttributeプロパティはfalse、派生クラスおよび基底クラス メソッドをオーバーライドする派生クラスのメンバーによって継承されません。

C#
[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
{ }

次に、2 つの基底クラスを定義します。 1 つ目のメソッド BaseAは、1 つのメソッドです MethodA。 2 つ目のメソッドは、 BaseB1 つのメソッドです MethodBBaseA属性MethodAInheritedAttributeタグ付けされ、BaseB``MethodB属性でNotInheritedAttributeタグ付けされます。 DerivedA はメソッドを BaseA 継承し、オーバーライド MethodA します。 DerivedB はメソッドを BaseB 継承し、オーバーライド MethodB します。

C#
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

この例の出力が示すように、DerivedA属性をInheritedAttribute継承しますDerivedA.MethodADerivedB``DerivedB.MethodB、属性はNotInheritedAttribute継承しません。

注釈

このプロパティは Inherited 次を決定します。

  • 属性が適用される属性でタグ付けされた基底クラスから派生したクラスがその属性を AttributeUsageAttribute 継承するかどうか。

  • 属性が適用される属性でタグ付けされた基底クラス メソッドをオーバーライドする派生クラスのメソッドがその属性を AttributeUsageAttribute 継承するかどうか。 (クラスが基底クラス メンバーを継承する場合、そのメンバーに適用されるすべての属性も継承されます)。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください