-
Notifications
You must be signed in to change notification settings - Fork 6k
Enhance Attribute Retrieval Guide to Include Member-Level Attributes #44665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d1194e
Fixed bug 40174.
f99951f
Resolving comments.
shethaadit 507fb8b
Resolving comments.
shethaadit e603f5c
Resolving comments.
shethaadit 40f7460
Moved code to different folder.
bebcaf5
Added example section.
e308de4
Added snippet in new file.
1f04017
Build newly added file.
95fc8bc
removed extra space.
92e3249
Added csproj.
d3569cb
Prevent auto-generate AssemblyInfo.cs.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...es/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/conceptualAttrUsage.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<StartupObject>AttributeRetrieval</StartupObject> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
samples/snippets/csharp/VS_Snippets_CLR/conceptual.attributes.usage/cs/source4.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//<snippet21> | ||
BillWagner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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."); | ||
} | ||
} | ||
} | ||
//</snippet21> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.