AttributeUsageAttribute.Inherited 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 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
属性值
如果该属性可以由派生类和重写成员继承,则为 true
;否则为 false
。 默认值为 true
。
示例
下面的示例说明了属性值为的特性应用于的特性与 AttributeUsageAttribute Inherited 应用了 true
其 AttributeUsageAttribute Inherited 属性值的 false
特性之间的差异。 此示例定义了两个属性: InheritedAttribute
和 NotInheritedAttribute
。 这两个特性可应用于类和方法。 由于应用于的 Inherited 特性的属性 AttributeUsageAttribute InheritedAttribute
为,因此 true
它由派生类和重写基类方法的派生类的成员继承。 另一方面,因为应用于的属性的 Inherited 属性 AttributeUsageAttribute NotInheritedAttribute
为 false
,所以它不会由派生类和重写基类方法的派生类的成员继承。
[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
继承自 BaseA
并重写其 MethodA
方法。 DerivedB
继承自 BaseB
并重写其 MethodB
方法。
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 继承了该特性。 (如果某个类继承基类成员,则它还将继承应用于该成员的任何属性。 )