打开发出的程序集生成的代码在 Reflector 中显示为空,而实际上并非如此。

发布于 2024-09-27 20:16:12 字数 744 浏览 1 评论 0原文

我正在使用 Reflection.Emit 生成一个动态程序集,其中包含一个类。我有一个错误导致 BadImageException。为了解决这个问题,我需要查看编译后的代码,因此我将动态程序集保存到磁盘。

我已经对程序集尝试过 PEVerify,它似乎认为没有错误。我现在想在 Reflector 中查看生成的代码,但程序集显示为空(我知道它不是)。

知道为什么会发生这种情况吗?

var assemblyName = new AssemblyName("An.Assembly");
var appDomain = Thread.GetDomain();
var assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
var typeBuilder = moduleBuilder.DefineType("MyClass", TypeAttributes.Public | TypeAttributes.Class);
... 
typeBuilder.CreateType();
assemblyBuilder.Save("temp.dll");

顺便说一下,我已经在使用反射器的 Relection.Emit 插件,但这对解决这个问题没有帮助。

I'm generating a dynamic assembly using Reflection.Emit which includes a single class. I have a bug which is causing a BadImageException. To resolve this I need to see the compiled code, and therefore I'm saving the dynamic assembly to disk.

I've already tried PEVerify against the assembly which seems to think there are no errors. I now want to view the generated code in Reflector, but the assembly appears as empty (which I know it's not).

Any idea why this is happening?

var assemblyName = new AssemblyName("An.Assembly");
var appDomain = Thread.GetDomain();
var assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
var typeBuilder = moduleBuilder.DefineType("MyClass", TypeAttributes.Public | TypeAttributes.Class);
... 
typeBuilder.CreateType();
assemblyBuilder.Save("temp.dll");

By the way I'm already using the Relection.Emit plugin for reflector which doesn't help with this issue.

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

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

发布评论

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

评论(1

药祭#氼 2024-10-04 20:16:12

替换:

var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

为:

var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, "yourfilename.dll");

然后在当前目录(可能是 bin 文件夹)中查找它。从技术上讲,程序集和模块是两个独立的实体,您仅保存程序集信息,而不是模块信息(所有代码所在的位置)。 (当然,不要将它们命名为相同的文件名。)

Replace:

var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

With:

var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name, "yourfilename.dll");

Then look for it in your current directory, probably your bin folder. Assemblies and modules are technically two separate entities, and you're only saving the assembly information, not the module information (where all your code lives). (Also, don't name them both with the same filename, of course.)

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