From 5d1194e529ace54b9cff12eb7450cf27b4dbc64f Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Mon, 3 Feb 2025 10:35:54 -0800 Subject: [PATCH 01/11] Fixed bug 40174. --- ...ieving-information-stored-in-attributes.md | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index 48c8942845941..540777bb0b135 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -1,6 +1,6 @@ --- title: "Retrieving Information Stored in Attributes" -description: Learn to retrieve information stored in attributes, such as for an attribute instance, many instances for the same scope, & many instances for different scopes. +description: Learn to retrieve information stored in attributes, including for an attribute instance, multiple instances for the same scope, multiple instances for different scopes, and attributes applied to class members. ms.date: "08/05/2022" ms.custom: devdivchpfy22 dev_langs: @@ -82,7 +82,50 @@ The attribute was not found. If no instances of the `DeveloperAttribute` are found on the method level or class level, the `GetAttribute` method notifies the user that no attributes were found and displays the name of the method or class that doesn't contain the attribute. If an attribute is found, the console displays the `Name`, `Level`, and `Reviewed` fields. - You can use the members of the class to get the individual methods and members in the passed class. This example first queries the `Type` object to get attribute information for the class level. Next, it uses to place instances of all methods into an array of objects to retrieve attribute information for the method level. You can also use the method to check for attributes on the property level or to check for attributes on the constructor level. + You can use the members of the class to get the individual methods and members in the passed class. This example first queries the `Type` object to get attribute information for the class level. Next, it uses to place instances of all methods into an array of objects to retrieve attribute information for the method level. You can also use the method to check for attributes on the property level or to check for attributes on the constructor level. + +## Retrieving Attributes from Class Members + +In addition to retrieving attributes at the class level, attributes can also be applied to individual members such as methods, properties, and fields. The `GetCustomAttribute` and `GetCustomAttributes` methods can be used to retrieve these attributes. + +### Example: Retrieving an Attribute from a Method + +The following example demonstrates how to retrieve an attribute applied to a method: + +```csharp +using System; +using System.Reflection; + +[AttributeUsage(AttributeTargets.Method)] +public class MyAttribute : Attribute +{ + public string Description { get; } + public MyAttribute(string description) => Description = description; +} + +public class MyClass +{ + [MyAttribute("This is a sample method.")] + public void MyMethod() { } +} + +class Program +{ + static void Main() + { + MethodInfo methodInfo = typeof(MyClass).GetMethod("MyMethod"); + MyAttribute? attribute = (MyAttribute?)Attribute.GetCustomAttribute(methodInfo, typeof(MyAttribute)); + + if (attribute != null) + { + Console.WriteLine($"Method Attribute: {attribute.Description}"); + } + else + { + Console.WriteLine("Attribute not found."); + } + } +} ## See also From f99951fb7c86ea5d46a5bd977218f36ed7e66243 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 4 Feb 2025 11:32:23 -0800 Subject: [PATCH 02/11] Resolving comments. Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- .../attributes/retrieving-information-stored-in-attributes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index 540777bb0b135..e3ba821945076 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -84,7 +84,7 @@ The attribute was not found. You can use the members of the class to get the individual methods and members in the passed class. This example first queries the `Type` object to get attribute information for the class level. Next, it uses to place instances of all methods into an array of objects to retrieve attribute information for the method level. You can also use the method to check for attributes on the property level or to check for attributes on the constructor level. -## Retrieving Attributes from Class Members +## Retrieving attributes from class members In addition to retrieving attributes at the class level, attributes can also be applied to individual members such as methods, properties, and fields. The `GetCustomAttribute` and `GetCustomAttributes` methods can be used to retrieve these attributes. From 507fb8b3585289f882e534b7dab9748123740d4c Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 4 Feb 2025 11:32:56 -0800 Subject: [PATCH 03/11] Resolving comments. Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- .../attributes/retrieving-information-stored-in-attributes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index e3ba821945076..d81f0fb5f4439 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -88,7 +88,7 @@ The attribute was not found. In addition to retrieving attributes at the class level, attributes can also be applied to individual members such as methods, properties, and fields. The `GetCustomAttribute` and `GetCustomAttributes` methods can be used to retrieve these attributes. -### Example: Retrieving an Attribute from a Method +### Example The following example demonstrates how to retrieve an attribute applied to a method: From e603f5c6cc0622bc3ac9fb85e2859c5c5dda1a7a Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Tue, 4 Feb 2025 11:33:22 -0800 Subject: [PATCH 04/11] Resolving comments. Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- .../attributes/retrieving-information-stored-in-attributes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index d81f0fb5f4439..a910701561b76 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -126,6 +126,7 @@ class Program } } } +``` ## See also From 40f7460d482cdef14afe6d53260571e794591f5d Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 5 Feb 2025 09:22:48 -0800 Subject: [PATCH 05/11] Moved code to different folder. --- ...ieving-information-stored-in-attributes.md | 40 +---------------- .../cpp/source3.cpp | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index a910701561b76..db6660f60d28e 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -84,49 +84,13 @@ The attribute was not found. You can use the members of the class to get the individual methods and members in the passed class. This example first queries the `Type` object to get attribute information for the class level. Next, it uses to place instances of all methods into an array of objects to retrieve attribute information for the method level. You can also use the method to check for attributes on the property level or to check for attributes on the constructor level. -## Retrieving attributes from class members +## Retrieving Attributes from Class Members In addition to retrieving attributes at the class level, attributes can also be applied to individual members such as methods, properties, and fields. The `GetCustomAttribute` and `GetCustomAttributes` methods can be used to retrieve these attributes. -### Example - The following example demonstrates how to retrieve an attribute applied to a method: -```csharp -using System; -using System.Reflection; - -[AttributeUsage(AttributeTargets.Method)] -public class MyAttribute : Attribute -{ - public string Description { get; } - public MyAttribute(string description) => Description = description; -} - -public class MyClass -{ - [MyAttribute("This is a sample method.")] - public void MyMethod() { } -} - -class Program -{ - static void Main() - { - MethodInfo methodInfo = typeof(MyClass).GetMethod("MyMethod"); - MyAttribute? attribute = (MyAttribute?)Attribute.GetCustomAttribute(methodInfo, typeof(MyAttribute)); - - if (attribute != null) - { - Console.WriteLine($"Method Attribute: {attribute.Description}"); - } - else - { - Console.WriteLine("Attribute not found."); - } - } -} -``` +[!code-csharp[Conceptual.Attributes.Usage#21](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs#21)] ## See also diff --git a/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp b/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp index 3837ecce1972b..f745f756d11b3 100644 --- a/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp +++ b/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp @@ -118,7 +118,50 @@ ref class GetAttribTest2 // }; +// +// This snippet demonstrates retrieving attributes from class members such as methods. + +using namespace System; +using namespace System::Reflection; + +[AttributeUsage(AttributeTargets::Method)] +public ref class MyAttribute : Attribute +{ +public: + String^ Description; + MyAttribute(String^ description) { Description = description; } +}; + +public ref class MyClass +{ +public: + [MyAttribute("This is a sample method.")] + void MyMethod() { } +}; + +ref class AttributeRetrieval +{ +public: + static void Main() + { + MethodInfo^ methodInfo = MyClass::typeid->GetMethod("MyMethod"); + MyAttribute^ attribute = (MyAttribute^)Attribute::GetCustomAttribute(methodInfo, MyAttribute::typeid); + + if (attribute != nullptr) + { + Console::WriteLine("Method Attribute: {0}", attribute->Description); + } + else + { + Console::WriteLine("Attribute not found."); + } + } +}; + +// + int main() { MainApp::Main(); + AttributeRetrieval::Main(); } From bebcaf5a275a4dc2990128e1c8f614d8e65be498 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 5 Feb 2025 09:24:39 -0800 Subject: [PATCH 06/11] Added example section. --- .../attributes/retrieving-information-stored-in-attributes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index db6660f60d28e..f178f234fbb17 100644 --- a/docs/standard/attributes/retrieving-information-stored-in-attributes.md +++ b/docs/standard/attributes/retrieving-information-stored-in-attributes.md @@ -88,6 +88,8 @@ The attribute was not found. In addition to retrieving attributes at the class level, attributes can also be applied to individual members such as methods, properties, and fields. The `GetCustomAttribute` and `GetCustomAttributes` methods can be used to retrieve these attributes. +### Example + The following example demonstrates how to retrieve an attribute applied to a method: [!code-csharp[Conceptual.Attributes.Usage#21](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs#21)] From e308de422cfb1f992f393bc94cd1c9324668ecf5 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 5 Feb 2025 10:07:52 -0800 Subject: [PATCH 07/11] Added snippet in new file. --- .../cpp/source3.cpp | 43 ------------------- .../conceptual.attributes.usage/cs/source4.cs | 40 +++++++++++++++++ 2 files changed, 40 insertions(+), 43 deletions(-) create mode 100644 samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs diff --git a/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp b/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp index f745f756d11b3..3837ecce1972b 100644 --- a/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp +++ b/samples/snippets/cpp/VS_Snippets_CLR/conceptual.attributes.usage/cpp/source3.cpp @@ -118,50 +118,7 @@ ref class GetAttribTest2 // }; -// -// This snippet demonstrates retrieving attributes from class members such as methods. - -using namespace System; -using namespace System::Reflection; - -[AttributeUsage(AttributeTargets::Method)] -public ref class MyAttribute : Attribute -{ -public: - String^ Description; - MyAttribute(String^ description) { Description = description; } -}; - -public ref class MyClass -{ -public: - [MyAttribute("This is a sample method.")] - void MyMethod() { } -}; - -ref class AttributeRetrieval -{ -public: - static void Main() - { - MethodInfo^ methodInfo = MyClass::typeid->GetMethod("MyMethod"); - MyAttribute^ attribute = (MyAttribute^)Attribute::GetCustomAttribute(methodInfo, MyAttribute::typeid); - - if (attribute != nullptr) - { - Console::WriteLine("Method Attribute: {0}", attribute->Description); - } - else - { - Console::WriteLine("Attribute not found."); - } - } -}; - -// - int main() { MainApp::Main(); - AttributeRetrieval::Main(); } diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs new file mode 100644 index 0000000000000..53a60dd093c72 --- /dev/null +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs @@ -0,0 +1,40 @@ +// +using System; +using System.Reflection; + +[AttributeUsage(AttributeTargets.Method)] +public class MyAttribute : Attribute +{ + public string Description { get; } + public MyAttribute(string description) { Description = description; } +} + +public class MyClass +{ + [MyAttribute("This is a sample method.")] + public void MyMethod() { } +} + +class AttributeRetrieval +{ + public static void Main() + { + // Create an instance of MyClass + MyClass myClass = new MyClass(); + + // Retrieve the method information for MyMethod + MethodInfo methodInfo = typeof(MyClass).GetMethod("MyMethod"); + MyAttribute attribute = (MyAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(MyAttribute)); + + if (attribute != null) + { + // Print the description of the method attribute + Console.WriteLine("Method Attribute: {0}", attribute.Description); + } + else + { + Console.WriteLine("Attribute not found."); + } + } +} +// From 1f040179bb870e5e1ca51772ace4b51b2be4c500 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 5 Feb 2025 14:21:31 -0800 Subject: [PATCH 08/11] Build newly added file. --- .../VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile index cd2eff1878c69..799f6f57fe082 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile @@ -1,4 +1,4 @@ -all: source1.exe source2.dll source3.exe +all: source1.exe source2.dll source3.exe source4.exe source1.exe: source1.cs csc source1.cs @@ -9,3 +9,7 @@ source2.dll: source2.cs source3.exe: source2.dll source3.cs csc /r:source2.dll source3.cs +source4.exe: source4.cs + csc source4.cs + + From 95fc8bcde0dd12863aab39186e89873f7ee3c1f2 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Wed, 5 Feb 2025 14:22:24 -0800 Subject: [PATCH 09/11] removed extra space. --- .../VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile index 799f6f57fe082..24bc3cdc22062 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile @@ -11,5 +11,3 @@ source3.exe: source2.dll source3.cs source4.exe: source4.cs csc source4.cs - - From 92e3249bb45cf87b16ddd36d3ebd43e63853587f Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 6 Feb 2025 11:22:04 -0800 Subject: [PATCH 10/11] Added csproj. --- .../conceptualAttrUsage.csproj | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj new file mode 100644 index 0000000000000..5daf331e824e1 --- /dev/null +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + enable + enable + AttributeRetrieval + + + From d3569cb5fe889616bf26e74573df17689982ef8c Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 6 Feb 2025 14:14:33 -0800 Subject: [PATCH 11/11] Prevent auto-generate AssemblyInfo.cs. --- .../conceptual.attributes.usage/conceptualAttrUsage.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj index 5daf331e824e1..759468c67814d 100644 --- a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj @@ -6,6 +6,7 @@ enable enable AttributeRetrieval + false