如何使用 IMetaDataDispenser.OpenScope 访问嵌入式程序集的元数据?
我有一个由多个项目组成的.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您无法使用 .NET Framework 内置函数来执行此操作,因为它们依赖于物理文件。但是,有一个使用 Mono Cecil 库的解决方案,因为它有一个需要 Stream 的重载作为其符号读取器的输入而不是文件路径。
下面是一个名为“TestPdb”的控制台应用程序的示例,它将其 IL 代码转储到控制台,包括 PDB 信息:
注意:由于您只需要从 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:
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.