Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,64 @@
[InheritedAttribute]
public class BaseA
{
[InheritedAttribute]
public virtual void MethodA()
{}
[InheritedAttribute]
public virtual void MethodA()
{ }
}

public class DerivedA : BaseA
{
public override void MethodA()
{}
}
public override void MethodA()
{ }
}

[NotInheritedAttribute]
[NotInheritedAttribute]
public class BaseB
{
[NotInheritedAttribute]
public virtual void MethodB()
{}
[NotInheritedAttribute]
public virtual void MethodB()
{ }
}

public class DerivedB : BaseB
{
public override void MethodB()
{}
public override void MethodB()
{ }
}

public class Example
{
public static void Main()
{
Type typeA = typeof(DerivedA);
Console.WriteLine("DerivedA has Inherited attribute: {0}",
typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberA = typeA.GetMethod("MethodA");
Console.WriteLine("DerivedA.MemberA has Inherited attribute: {0}\n",
memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);

Type typeB = typeof(DerivedB);
Console.WriteLine("DerivedB has Inherited attribute: {0}",
typeB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberB = typeB.GetMethod("MethodB");
Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}",
memberB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
}
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 Inherited attribute: False
// DerivedB.MemberB has Inherited attribute: False
// DerivedB has NotInherited attribute: False
// DerivedB.MemberB has NotInherited attribute: False
// </Snippet2>

// <Snippet1>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = true)]
public class InheritedAttribute : Attribute
{}
{ }

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
AttributeTargets.Property | AttributeTargets.Field,
Inherited = false)]
public class NotInheritedAttribute : Attribute
{}
// </Snippet1>
{ }
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,38 @@ Option Strict On
Imports System.Reflection

<InheritedAttribute> Public Class BaseA
<InheritedAttribute> Public Overridable Sub MethodA()
End Sub
<InheritedAttribute> Public Overridable Sub MethodA()
End Sub
End Class

Public Class DerivedA : Inherits BaseA
Public Overrides Sub MethodA
End Sub
End Class
Public Overrides Sub MethodA()
End Sub
End Class

<NotInheritedAttribute> Public Class BaseB
<NotInheritedAttribute> Public Overridable Sub MethodB()
End Sub
<NotInheritedAttribute> Public Overridable Sub MethodB()
End Sub
End Class

Public Class DerivedB : Inherits BaseB
Public Overrides Sub MethodB
End Sub
End Class
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
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
Expand All @@ -51,15 +47,15 @@ End Module
' </Snippet2>

' <Snippet1>
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method _
Or AttributeTargets.Property Or AttributeTargets.Field,
Inherited := True)>
<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)>
Or AttributeTargets.Property Or AttributeTargets.Field,
Inherited:=False)>
Public Class NotInheritedAttribute : Inherits Attribute
End Class
' </Snippet1>
Expand Down