Skip to content

Commit 90c1904

Browse files
shethaaditAdit ShethBartoszKlonowski
authored
Enhance Attribute Retrieval Guide to Include Member-Level Attributes (#44665)
* Fixed bug 40174. * Resolving comments. Co-authored-by: Bartosz Klonowski <[email protected]> * Resolving comments. Co-authored-by: Bartosz Klonowski <[email protected]> * Resolving comments. Co-authored-by: Bartosz Klonowski <[email protected]> * Moved code to different folder. * Added example section. * Added snippet in new file. * Build newly added file. * removed extra space. * Added csproj. * Prevent auto-generate AssemblyInfo.cs. --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Bartosz Klonowski <[email protected]>
1 parent 718ff51 commit 90c1904

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

docs/standard/attributes/retrieving-information-stored-in-attributes.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Retrieving Information Stored in Attributes"
3-
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.
3+
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.
44
ms.date: "08/05/2022"
55
ms.custom: devdivchpfy22
66
dev_langs:
@@ -82,7 +82,17 @@ The attribute was not found.
8282

8383
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.
8484

85-
You can use the members of the <xref:System.Type> 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 <xref:System.Type.GetMethods%2A?displayProperty=nameWithType> to place instances of all methods into an array of <xref:System.Reflection.MemberInfo?displayProperty=nameWithType> objects to retrieve attribute information for the method level. You can also use the <xref:System.Type.GetProperties%2A?displayProperty=nameWithType> method to check for attributes on the property level or <xref:System.Type.GetConstructors%2A?displayProperty=nameWithType> to check for attributes on the constructor level.
85+
You can use the members of the <xref:System.Type> 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 <xref:System.Type.GetMethods%2A?displayProperty=nameWithType> to place instances of all methods into an array of <xref:System.Reflection.MemberInfo?displayProperty=nameWithType> objects to retrieve attribute information for the method level. You can also use the <xref:System.Type.GetProperties%2A?displayProperty=nameWithType> method to check for attributes on the property level or <xref:System.Type.GetConstructors%2A?displayProperty=nameWithType> to check for attributes on the constructor level.
86+
87+
## Retrieving Attributes from Class Members
88+
89+
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.
90+
91+
### Example
92+
93+
The following example demonstrates how to retrieve an attribute applied to a method:
94+
95+
[!code-csharp[Conceptual.Attributes.Usage#21](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs#21)]
8696

8797
## See also
8898

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<StartupObject>AttributeRetrieval</StartupObject>
9+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
10+
</PropertyGroup>
11+
12+
</Project>

samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
all: source1.exe source2.dll source3.exe
1+
all: source1.exe source2.dll source3.exe source4.exe
22

33
source1.exe: source1.cs
44
csc source1.cs
@@ -9,3 +9,5 @@ source2.dll: source2.cs
99
source3.exe: source2.dll source3.cs
1010
csc /r:source2.dll source3.cs
1111

12+
source4.exe: source4.cs
13+
csc source4.cs
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//<snippet21>
2+
using System;
3+
using System.Reflection;
4+
5+
[AttributeUsage(AttributeTargets.Method)]
6+
public class MyAttribute : Attribute
7+
{
8+
public string Description { get; }
9+
public MyAttribute(string description) { Description = description; }
10+
}
11+
12+
public class MyClass
13+
{
14+
[MyAttribute("This is a sample method.")]
15+
public void MyMethod() { }
16+
}
17+
18+
class AttributeRetrieval
19+
{
20+
public static void Main()
21+
{
22+
// Create an instance of MyClass
23+
MyClass myClass = new MyClass();
24+
25+
// Retrieve the method information for MyMethod
26+
MethodInfo methodInfo = typeof(MyClass).GetMethod("MyMethod");
27+
MyAttribute attribute = (MyAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(MyAttribute));
28+
29+
if (attribute != null)
30+
{
31+
// Print the description of the method attribute
32+
Console.WriteLine("Method Attribute: {0}", attribute.Description);
33+
}
34+
else
35+
{
36+
Console.WriteLine("Attribute not found.");
37+
}
38+
}
39+
}
40+
//</snippet21>

0 commit comments

Comments
 (0)