How to get access to embedded assembly's metadata using IMetaDataDispenser.OpenScope?

强颜欢笑 提交于 2019-12-11 12:40:36

问题


I have a .NET solution which consists of several projects. It's possible to say that one of these projects is logically a primary one and all others are secondary. Our team has decided to build the project the next way. The main project will produce an assembly (I'll refer it to as Primary). All other projects' assemblies are Secondary and they will be embedded as a resource into the Primary one.

The SourceCodeForExceptionHelper class in the Primary project is responsible for getting the original source code using PDB files on every encountered exception. To do that I use the approach described here. It worked correctly in my separate proof of concept project. But when I tried to move that class into the real solution I've encountered a problem: the IMetaDataDispenser.OpenScope method requires not null reference to assembly file's path. Surely, I haven't such a reference for any of Secondary assembly (because their files are embedded in the Primary). For that reason I can't create an object of the type ISymbolReader and read the source code. How can I solve that problem? By the way, the problem is even worse because we embed only Secondary assemblies without their PDB files (though we will do it if it is necessary).

Thanks in advance for any help and advice!


回答1:


I don't think you can do this using the .NET Framework builtin functions, as they rely on physical files. However, there is a solution using the Mono Cecil library, as it has an overloads that takes a Stream as input instead of a file path for its symbols reader.

Here is an example of a Console app named "TestPdb" which dumps its IL code to the console, including PDB information:

using System;
using System.IO;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Pdb;

namespace TestPdb
{
    class Program
    {
        static void Main(string[] args)
        {
            // we use a Stream for the assembly
            AssemblyDefinition asm;
            using (FileStream asmStream = new FileStream("testpdb.exe", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                asm = AssemblyDefinition.ReadAssembly(asmStream);
            }

            // we use a Stream for the PDB
            using (FileStream symbolStream = new FileStream("testpdb.pdb", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                asm.MainModule.ReadSymbols(new PdbReaderProvider().GetSymbolReader(asm.MainModule, symbolStream));
            }

            TypeDefinition type = asm.MainModule.GetType("TestPdb.Program");

            foreach (MethodDefinition method in type.Methods)
            {
                Console.WriteLine("Method:" + method.Name);
                foreach (Instruction ins in method.Body.Instructions)
                {
                    Console.WriteLine(" " + ins);
                    if (ins.SequencePoint != null)
                    {
                        Console.WriteLine("  Url:" + ins.SequencePoint.Document.Url);
                        // see http://blogs.msdn.com/b/jmstall/archive/2005/06/19/feefee-sequencepoints.aspx
                        if (ins.SequencePoint.StartLine != 0xFEEFEE)
                        {
                            Console.WriteLine("  StartLine:" + ins.SequencePoint.StartLine + " StartColumn:" + ins.SequencePoint.StartColumn);
                            Console.WriteLine("  EndLine:" + ins.SequencePoint.EndLine + " EndColumn:" + ins.SequencePoint.EndColumn);
                        }
                        // etc...
                    }
                }
            }   
        }
    }
}

NOTE: since you only need to read from PDBs, you can recompile the Cecil library defining the READ_ONLY conditional symbol to save some bytes. You can also embed Cecil source code directly in your assemblies, no need to use the .DLL versions.



来源:https://stackoverflow.com/questions/7570260/how-to-get-access-to-embedded-assemblys-metadata-using-imetadatadispenser-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!