AttributeUsageAttribute.Inherited Property

 

Gets or sets a Boolean value that determines whether the indicated attribute is inherited by derived classes and overriding members.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Property Inherited As Boolean

Property Value

Type: System.Boolean

true if the attribute can be inherited by derived classes and overriding members; otherwise, false. The default is true.

The Inherited property determines:

  • Whether classes derived from a base class tagged with the attribute to which the AttributeUsageAttribute attribute is applied inherit that attribute.

  • 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.)

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 true is applied . The example defines two attributes, InheritedAttribute and NotInheritedAttribute. Both attributes can apply to classes and methods. 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. 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 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. The first, BaseA, has a single method, MethodA. The second, BaseB, has a single method, MethodB. BaseA and MethodA are tagged with the InheritedAttribute attribute, and BaseB and MethodB are tagged with the NotInheritedAttribute attribute. DerivedA inherits from BaseA and overrides its MethodA method. DerivedB inherits from BaseB and overrides its MethodB method.

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: {0}", 
                        typeA.GetCustomAttributes(GetType(InheritedAttribute), True).Length > 0) 
      Dim memberA As MethodInfo = typeA.GetMethod("MethodA")
      Console.WriteLine("DerivedA.MemberA has Inherited attribute: {0}", 
                        memberA.GetCustomAttributes(GetType(InheritedAttribute), True).Length > 0) 
      Console.WriteLine()

      Dim typeB As Type = GetType(DerivedB)
      Console.WriteLine("DerivedB has Inherited attribute: {0}", 
                        typeB.GetCustomAttributes(GetType(InheritedAttribute), True).Length > 0) 
      Dim memberB As MethodInfo = typeB.GetMethod("MethodB")
      Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}", 
                        memberB.GetCustomAttributes(GetType(InheritedAttribute), 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

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: