From 485f4bd36aebab140506475f7a74ca9027a90f0a Mon Sep 17 00:00:00 2001 From: Alexandr Tsvetkov Date: Thu, 15 Mar 2018 17:59:18 +0100 Subject: [PATCH 1/4] Change check NotInheritedAttribute Change check InheritedAttribute for classes BaseB and DerivedB to check NotInheritedAttribute --- .../cs/Inherited1.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs index 1659f862f72d4..0c1566101ad4a 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs +++ b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs @@ -42,19 +42,19 @@ public static void Main() 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); + Console.WriteLine("DerivedB has NotInherited attribute: {0}", + typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0); MethodInfo memberB = typeB.GetMethod("MethodB"); - Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}", - memberB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0); + Console.WriteLine("DerivedB.MemberB has NotInherited attribute: {0}", + 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 // // @@ -69,4 +69,4 @@ public class InheritedAttribute : Attribute Inherited = false)] public class NotInheritedAttribute : Attribute {} -// \ No newline at end of file +// From 902af05fcbf00090ee0db61ed67a6b25d81678a7 Mon Sep 17 00:00:00 2001 From: Alexandr Tsvetkov Date: Mon, 19 Mar 2018 10:34:45 +0100 Subject: [PATCH 2/4] Added nameof operator to ge method's name --- .../System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs index 0c1566101ad4a..15cdb2f2ae9da 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs +++ b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs @@ -37,14 +37,14 @@ 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"); + MethodInfo memberA = typeA.GetMethod(nameof(DerivedA.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 NotInherited attribute: {0}", typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0); - MethodInfo memberB = typeB.GetMethod("MethodB"); + MethodInfo memberB = typeB.GetMethod(nameof(DerivedB.MethodB)); Console.WriteLine("DerivedB.MemberB has NotInherited attribute: {0}", memberB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0); } From 3667b1fb339d676166f03e83ac65e9ea0a19d51e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 19 Mar 2018 10:40:40 -0400 Subject: [PATCH 3/4] update for interpolated strings Found and updated while added the VB version --- .../cs/Inherited1.cs | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs index 15cdb2f2ae9da..8b4921691962c 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs +++ b/samples/snippets/csharp/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/cs/Inherited1.cs @@ -5,49 +5,45 @@ [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(nameof(DerivedA.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 NotInherited attribute: {0}", - typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0); - MethodInfo memberB = typeB.GetMethod(nameof(DerivedB.MethodB)); - Console.WriteLine("DerivedB.MemberB has NotInherited attribute: {0}", - memberB.GetCustomAttributes(typeof(NotInheritedAttribute), 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 @@ -58,15 +54,15 @@ public static void Main() // // -[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 -{} +{ } // From 1d7a4349df1c607f6716c6209b046d684315ff26 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 19 Mar 2018 10:42:23 -0400 Subject: [PATCH 4/4] update to match C# changes Also, switch to using interpolated strings. --- .../vb/Inherited1.vb | 58 +++++++++---------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb b/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb index 675746bcfa163..7f3a437d21698 100644 --- a/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb +++ b/samples/snippets/visualbasic/VS_Snippets_CLR_System/System.AttributeUsageAttribute.Inherited/vb/Inherited1.vb @@ -5,42 +5,38 @@ Option Strict On Imports System.Reflection Public Class BaseA - Public Overridable Sub MethodA() - End Sub + 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 Public Class BaseB - Public Overridable Sub MethodB() - End Sub + 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 @@ -51,15 +47,15 @@ End Module ' ' - + Public Class InheritedAttribute : Inherits Attribute End Class + Or AttributeTargets.Property Or AttributeTargets.Field, + Inherited:=False)> Public Class NotInheritedAttribute : Inherits Attribute End Class '