Skip to content
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

Add documentation for CustomAttrbute #9175

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gewarren marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;

static class CustomAttributeSnippets
{
// <SnippetPrintAttributes>
static void PrintCustomAttributes(MetadataReader mr, TypeDefinition t)
{
// Enumerate custom attributes on the type definition
foreach (CustomAttributeHandle attrHandle in t.GetCustomAttributes())
{
CustomAttribute attr = mr.GetCustomAttribute(attrHandle);

// Display the attribute type full name
if (attr.Constructor.Kind == HandleKind.MethodDefinition)
{
MethodDefinition mdef = mr.GetMethodDefinition((MethodDefinitionHandle)attr.Constructor);
TypeDefinition tdef = mr.GetTypeDefinition(mdef.GetDeclaringType());
Console.WriteLine($"Type: {mr.GetString(tdef.Namespace)}.{mr.GetString(tdef.Name)}");
}
else if (attr.Constructor.Kind == HandleKind.MemberReference)
{
MemberReference mref = mr.GetMemberReference((MemberReferenceHandle)attr.Constructor);

if (mref.Parent.Kind == HandleKind.TypeReference)
{
TypeReference tref = mr.GetTypeReference((TypeReferenceHandle)mref.Parent);
Console.WriteLine($"Type: {mr.GetString(tref.Namespace)}.{mr.GetString(tref.Name)}");
}
else if (mref.Parent.Kind == HandleKind.TypeDefinition)
{
TypeDefinition tdef = mr.GetTypeDefinition((TypeDefinitionHandle)mref.Parent);
Console.WriteLine($"Type: {mr.GetString(tdef.Namespace)}.{mr.GetString(tdef.Name)}");
}
}

// Display the attribute value
byte[] data = mr.GetBlobBytes(attr.Value);
Console.Write("Value: ");

for (int i = 0; i < data.Length; i++) Console.Write($"{data[i]:X2} ");

Console.WriteLine();
}
}
// </SnippetPrintAttributes>

public static void Run()
{
using var fs = new FileStream(typeof(Program).Assembly.Location, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var peReader = new PEReader(fs);
MetadataReader mr = peReader.GetMetadataReader();

foreach(TypeDefinitionHandle tdh in mr.TypeDefinitions)
{
TypeDefinition t = mr.GetTypeDefinition(tdh);
Console.WriteLine($"{mr.GetString(t.Namespace)}.{mr.GetString(t.Name)}");
PrintCustomAttributes(mr, t);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

class Program
{
static void Main()
{
CustomAttributeSnippets.Run();
}
}
16 changes: 14 additions & 2 deletions xml/System.Reflection.Metadata/CustomAttribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@
</Attribute>
</Attributes>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
<summary>Provides information about a custom attribute.</summary>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks
A custom attribute is an annotation that associates additional information with a metadata element, such as an assembly, type, or method. You can use the <xref:System.Reflection.Metadata.MetadataReader.GetCustomAttribute(System.Reflection.Metadata.CustomAttributeHandle)> method to get a custom attribute instance. For more information about attributes in .NET, see [Extend metadata using attributes](/dotnet/standard/attributes/).

## Examples
This example shows how to print all custom attributes applied to the type definition:

:::code language="csharp" source="~/snippets/csharp/System.Reflection.Metadata/CustomAttribute/Overview/CustomAttributeSnippets.cs" id="SnippetPrintAttributes":::

]]></format>
</remarks>
</Docs>
<Members>
<Member MemberName="Constructor">
Expand Down