AttributeUsageAttribute.Inherited 속성

정의

지정된 특성이 파생된 클래스에서 상속되고 멤버를 재정의하는지 여부를 결정하는 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

속성 값

Boolean

지정된 특성이 파생된 클래스에서 상속되고 멤버를 재정의할 수 있으면 true이고, 그렇지 않으면 false입니다. 기본값은 true입니다.

예제

다음 예제에서는 속성 값이 적용된 AttributeUsageAttribute 특성과 Inherited 속성 값이 true 적용된 AttributeUsageAttribute 특성 간의 false 차이점을 Inherited 보여 줍니다. 이 예제에서는 두 가지 특성을 InheritedAttribute 정의하고 NotInheritedAttribute. 두 특성 모두 클래스와 메서드에 적용할 수 있습니다. 적용 InheritedAttributeInherited 특성의 AttributeUsageAttribute 속성은 true파생 클래스 및 기본 클래스 메서드를 재정의하는 파생 클래스의 멤버에 의해 상속됩니다. 반면 Inherited 에 적용 NotInheritedAttribute 된 특성의 속성은 false파생 클래스 및 기본 클래스 메서드를 재정의 AttributeUsageAttribute 하는 파생 클래스의 멤버에 의해 상속되지 않습니다.

[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는 해당 MethodA 메서드에서 BaseA 상속되고 재정의됩니다. DerivedB는 해당 MethodB 메서드에서 BaseB 상속되고 재정의됩니다.

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 메서드가 해당 특성을 상속하는지 여부입니다. (클래스가 기본 클래스 멤버를 상속하는 경우 해당 멤버에 적용된 모든 특성도 상속합니다.)

적용 대상

추가 정보