Recently I am running some DynamicMethod scenario.

It seems very easy to Emit incorrect IL using ILGenerator. If the IL is not correct, JIT will throw unverifiable code exception or invalid program exception.

I tend to debug with WinDBG+SOS for IL related issues in the past. However, for some reason, !dumpIL does not work as expected for dynamic method.

The best way to debug seems to be read source code and build the stack in mind. So here are a few mistakes I made. Hope you can avoid them.

1. Type mismatch on the stack

2. Stack underflow (caller expects one object on the stack, but callee returns with nothing left on the stack)

3. Stack overflow (caller expects stack to be empty, but callee leaves one object on the stack before it returns)

4. Signature not match (DynamicMethod returns a ref object, but delegate expects a valuetype)