diff --git a/docs/standard/attributes/retrieving-information-stored-in-attributes.md b/docs/standard/attributes/retrieving-information-stored-in-attributes.md index 48c8942845941..f178f234fbb17 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,17 @@ 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 + +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)] ## See also 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..759468c67814d --- /dev/null +++ b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj @@ -0,0 +1,12 @@ + + + + Exe + net8.0 + enable + enable + AttributeRetrieval + false + + + 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..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 @@ -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,5 @@ source2.dll: source2.cs source3.exe: source2.dll source3.cs csc /r:source2.dll source3.cs +source4.exe: source4.cs + csc source4.cs 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."); + } + } +} +//