如何使用 IMetaDataDispenser.OpenScope 访问嵌入式程序集的元数据?

发布于 2024-12-06 21:36:21 字数 267 浏览 0 评论 0原文

我有一个由多个项目组成的.NET 解决方案。可以说,这些项目之一在逻辑上是主要项目,而所有其他项目都是次要项目。我们的团队决定以下一步的方式构建该项目。主项目将生成一个程序集(我将其称为“主要”)。所有其他项目的程序集都是次要的,它们将作为资源嵌入到主要项目中。

Primary 项目中的 SourceCodeForExceptionHelper 类负责在每个遇到的异常上使用 PDB 文件获取原始源代码。为此,我使用描述的方法

预先感谢您的任何帮助和建议!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月松间行 2024-12-13 21:36:21

我认为您无法使用 .NET Framework 内置函数来执行此操作,因为它们依赖于物理文件。但是,有一个使用 Mono Cecil 库的解决方案,因为它有一个需要 Stream 的重载作为其符号读取器的输入而不是文件路径。

下面是一个名为“TestPdb”的控制台应用程序的示例,它将其 IL 代码转储到控制台,包括 PDB 信息:

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...
                    }
                }
            }   
        }
    }
}

注意:由于您只需要从 PDB 中读取数据,因此您可以重新编译定义 READ_ONLY 条件符号的 Cecil 库以保存一些信息。字节。您还可以将 Cecil 源代码直接嵌入到程序集中,无需使用 .DLL 版本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文