-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9207 from dotnet/main
- Loading branch information
Showing
8 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...ets/csharp/System.Reflection.Metadata/CustomAttribute/Overview/CustomAttributeSnippets.cs
This file contains 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,78 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
|
||
static class CustomAttributeSnippets | ||
{ | ||
// <SnippetPrintAttributes> | ||
class MyAttribute : Attribute | ||
{ | ||
public int X { get; set; } | ||
} | ||
|
||
[My(X = 1)] | ||
class ExampleType1 { } | ||
|
||
[My(X = 2)] | ||
class ExampleType2 { } | ||
|
||
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(); | ||
} | ||
} | ||
|
||
static void PrintTypesCustomAttributes(MetadataReader mr) | ||
{ | ||
foreach (TypeDefinitionHandle tdh in mr.TypeDefinitions) | ||
{ | ||
TypeDefinition t = mr.GetTypeDefinition(tdh); | ||
Console.WriteLine($"{mr.GetString(t.Namespace)}.{mr.GetString(t.Name)}"); | ||
PrintCustomAttributes(mr, t); | ||
} | ||
} | ||
// </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(); | ||
PrintTypesCustomAttributes(mr); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...csharp/System.Reflection.Metadata/CustomAttribute/Overview/CustomAttributeSnippets.csproj
This file contains 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,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
snippets/csharp/System.Reflection.Metadata/CustomAttribute/Overview/Program.cs
This file contains 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,9 @@ | ||
using System; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
CustomAttributeSnippets.Run(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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