diff --git a/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.cs b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.cs new file mode 100644 index 00000000000..897558faaa2 --- /dev/null +++ b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.cs @@ -0,0 +1,30 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Reflection.Metadata; +using System.Reflection.PortableExecutable; + +namespace MetadataReaderSnippets +{ + class MetadataReaderSnippets + { + public static void Run() + { + // + using var fs = new FileStream("Example.dll", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var peReader = new PEReader(fs); + + MetadataReader mr = peReader.GetMetadataReader(); + + foreach (TypeDefinitionHandle tdefh in mr.TypeDefinitions) + { + TypeDefinition tdef = mr.GetTypeDefinition(tdefh); + + string ns = mr.GetString(tdef.Namespace); + string name = mr.GetString(tdef.Name); + Console.WriteLine($"{ns}.{name}"); + } + // + } + } +} diff --git a/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.csproj b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.csproj new file mode 100644 index 00000000000..2be46cd754a --- /dev/null +++ b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/Program.cs b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/Program.cs new file mode 100644 index 00000000000..765cdd35b55 --- /dev/null +++ b/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace MetadataReaderSnippets +{ + class Program + { + static void Main() + { + MetadataReaderSnippets.Run(); + } + } +} diff --git a/xml/System.Reflection.Metadata/MetadataReader.xml b/xml/System.Reflection.Metadata/MetadataReader.xml index 11ec2fdd11b..ade7482de41 100644 --- a/xml/System.Reflection.Metadata/MetadataReader.xml +++ b/xml/System.Reflection.Metadata/MetadataReader.xml @@ -22,7 +22,23 @@ Reads metadata as defined by the ECMA 335 CLI specification. - To be added. + + reads the contents of tables and heaps from the specified CLI metadata. It operates low-level constructs such as type and method definitions. For a higher level API to inspect the contents of assemblies using reflection constructs, see . + +You can use constructors, such as , to create an instance of for a given memory location. To read metadata from the Portable Executable assembly file, create and use the extension method. + +The format of CLI metadata is defined by the ECMA-335 specification. For more information, see [Standard ECMA-335 - Common Language Infrastructure (CLI)](http://www.ecma-international.org/publications/standards/Ecma-335.htm) on the Ecma International Web site. + +## Examples +This example shows how to create for an assembly and read all type definitions from it: + +[!code-csharp[](~/samples/snippets/csharp/api/system.reflection.metadata/metadatareader/MetadataReaderSnippets.cs#MetadataReader)] + + ]]> +