Is there any way to instrument System.Reflection.Emit?

旧时模样 提交于 2019-12-23 00:41:07

问题


I'm having some serious trouble with a compiler that does its code generation with the classes found in System.Reflection.Emit. It takes code that should be perfectly valid, and that passes all the compiler's internal checks, and tries to codegen it, but TypeBuilder.CreateType() throws the following completely unhelpful exception:

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

The problem lies somewhere in the way it's transforming generators into state machines in a specific edge case, but if I take the post-transformation AST, turn it back to source code, and feed it into the compiler as-is, everything builds successfully and runs as expected, which means something very strange is going on.

The problem is, System.Reflection.Emit is a black box. It would be very nice if I could instrument it somehow and have it produce a step-by-step log of exactly what it's doing, so I can diff the working and non-working versions and see where the problem lies. I tried doing this manually, tracing through the codegen phase's execution step by step in the debugger and taking notes of what got called where, turning it into a script, but it took several days and I must have missed something somewhere, because I'm getting the same error even though it was the "working" version I was tracing through.

Does anyone know of a way to log System.Reflection.Emit so I can get a good idea of what's really going on here?

来源:https://stackoverflow.com/questions/34269925/is-there-any-way-to-instrument-system-reflection-emit

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