File size: 5,154 Bytes
8e6f15c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
## About

<!-- A description of the package and where one can find more documentation -->

This package provides a low-level .NET (ECMA-335) metadata reader and writer. It's geared for performance and is the ideal choice for building higher-level libraries that intend to provide their own object model, such as compilers. The metadata format is defined by the [ECMA-335 - Common Language Infrastructure (CLI)](http://www.ecma-international.org/publications/standards/Ecma-335.htm) specification and [its amendments](https://github.com/dotnet/runtime/blob/main/docs/design/specs/Ecma-335-Augments.md).

The `System.Reflection.Metadata` library is included in the .NET Runtime shared framework. The package can be installed when you need to use it in other target frameworks.

## How to Use

<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package -->

The following example shows how to read assembly information using PEReader and MetadataReader.

```cs

using System;

using System.IO;

using System.Reflection.Metadata;

using System.Reflection.PortableExecutable;



class Program

{

    static void Main()

    {

        // Open the Portable Executable (PE) file

        using var fs = new FileStream("Example.dll", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        using var peReader = new PEReader(fs);



        // Display PE header information

        PEHeader header = peReader.PEHeaders.PEHeader;

        Console.WriteLine($"Image base:     0x{header.ImageBase:X}");

        Console.WriteLine($"File alignment: 0x{header.FileAlignment:X}");

        Console.WriteLine($"Subsystem:      {header.Subsystem}");



        // Display .NET metadata information

        if (!peReader.HasMetadata)

        {

            Console.WriteLine("Image does not contain .NET metadata");

            return;

        }



        MetadataReader mr = peReader.GetMetadataReader();

        AssemblyDefinition ad = mr.GetAssemblyDefinition();

        Console.WriteLine($"Assembly name:  {ad.GetAssemblyName().ToString()}");

        Console.WriteLine();

        Console.WriteLine("Assembly attributes:");



        foreach (CustomAttributeHandle attrHandle in ad.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($"{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($"{mr.GetString(tref.Namespace)}.{mr.GetString(tref.Name)}");

                }

                else if (mref.Parent.Kind == HandleKind.TypeDefinition)

                {

                    TypeDefinition tdef = mr.GetTypeDefinition((TypeDefinitionHandle)mref.Parent);

                    Console.WriteLine($"{mr.GetString(tdef.Namespace)}.{mr.GetString(tdef.Name)}");

                }

            }

        }

    }

}



```

## Main Types

<!-- The main types provided in this library -->

The main types provided by this library are:

* `System.Reflection.Metadata.MetadataReader`
* `System.Reflection.PortableExecutable.PEReader`
* `System.Reflection.Metadata.Ecma335.MetadataBuilder`
* `System.Reflection.PortableExecutable.PEBuilder`
* `System.Reflection.PortableExecutable.ManagedPEBuilder`

## Additional Documentation

<!-- Links to further documentation -->

* [System.Reflection.Metadata.MetadataReader](https://docs.microsoft.com/dotnet/api/system.reflection.metadata.metadatareader)
* [System.Reflection.PortableExecutable.PEReader](https://docs.microsoft.com/dotnet/api/system.reflection.portableexecutable.pereader)
* [System.Reflection.Metadata.Ecma335.MetadataBuilder](https://docs.microsoft.com/dotnet/api/system.reflection.metadata.ecma335.metadatabuilder)
* [System.Reflection.PortableExecutable.PEBuilder](https://docs.microsoft.com/dotnet/api/system.reflection.portableexecutable.pebuilder)
* [System.Reflection.PortableExecutable.ManagedPEBuilder](https://docs.microsoft.com/dotnet/api/system.reflection.portableexecutable.managedpebuilder)

## Feedback & Contributing

<!-- How to provide feedback on this package and contribute to it -->

System.Reflection.Metadata is released as open source under the [MIT license](https://licenses.nuget.org/MIT). Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime).