Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.47k stars 4.8k forks source link

Use System.Reflection.Metadata to harvest type info without assembly loading #8139

Closed heaths closed 3 years ago

heaths commented 5 years ago

Sample code if you want to try Reflection.Metadata:

var assemblyPath = @"d:\github\azure\azure-sdk-for-net3\artifacts\bin\Azure.Core\Debug\net461\Azure.Core.dll";
using (Stream stream = File.OpenRead(assemblyPath))
using (PEReader reader = new PEReader(stream))
{
    var mr = reader.GetMetadataReader();
    foreach (var tdh in mr.TypeDefinitions)
    {
        var td = mr.GetTypeDefinition(tdh);
        if ((td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public)
        {
            Console.WriteLine(mr.GetString(td.Name));
        }
    }
}

Originally posted by @pakrym in https://github.com/Azure/azure-sdk-for-net/pull/8098#issuecomment-542740483

heaths commented 3 years ago

Closing this one. I can't even find the script for which this was intended. If it ever comes up again, we can use this other assembly to avoid all the assembly resolution I had to do in-proc using System.Reflection.