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이고, 그렇지 않으면 false입니다.true if the attribute can be inherited by derived classes and overriding members; otherwise, false. 기본값은 true입니다.The default is true.

예제

다음 예에서는 trueInherited 속성 값이 적용 된 AttributeUsageAttribute 특성이 적용 되는 특성과 Inherited 속성 값 false가 적용 된 AttributeUsageAttribute 특성이 적용 되는 특성의 차이점을 보여 줍니다.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라는 두 가지 특성을 정의 합니다.The example defines two attributes, InheritedAttribute and NotInheritedAttribute. 두 특성 클래스 및 메서드를 적용할 수 있습니다.Both attributes can apply to classes and methods. InheritedAttribute에 적용 되는 AttributeUsageAttribute 특성의 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. 반면에 NotInheritedAttribute에 적용 되는 AttributeUsageAttribute 특성의 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에는 MethodA단일 메서드가 있습니다.The first, BaseA, has a single method, MethodA. 두 번째 BaseB에는 MethodB단일 메서드가 있습니다.The second, BaseB, has a single method, MethodB. BaseAMethodAInheritedAttribute 특성으로 태그가 지정 되 고 BaseBMethodBNotInheritedAttribute 특성으로 태그가 지정 됩니다.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.MethodAInheritedAttribute 특성을 상속 하지만 DerivedBDerivedB.MethodBNotInheritedAttribute 특성을 상속 하지 않습니다.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.)

적용 대상

추가 정보