I’m encountering this annoying bug in Mono.Cecil… It seems that when modifying the assembly it calculates wrong offset, depending on what I add…
Basically Mono.Cecil adds a jump out of the method.
Which in short leads to broken assemblies:
[IL]: Error: [C:\…\Tedd.Fibres\Tedd.Fibres.Test.GUI\bin\Debug\Tedd.Fibres.TestModule_out.dll : Tedd.Fibres.TestModule.TestClass::Test][offset 0x00000028] Branch out of the method.
I managed to Google some other people having the same problem, and Mono.Cecil doesn’t seem to be under active development for the past 3 years or so… Hmm…. :/
UPDATE: This is not a bug!
As the good Jb Evain explained, simply add method.Body.Simplify() before and method.Body.Optimize() after the method processing and it works fine.