问题
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