打开发出的程序集生成的代码在 Reflector 中显示为空,而实际上并非如此。
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换:
为:
然后在当前目录(可能是 bin 文件夹)中查找它。从技术上讲,程序集和模块是两个独立的实体,您仅保存程序集信息,而不是模块信息(所有代码所在的位置)。 (当然,不要将它们命名为相同的文件名。)
Replace:
With:
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.)