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.
1 thought on “Bug in Mono.Cecil”
Cecil is being developed and maintained. And the bug is not directly in Cecil, but in how you use it. You’re injecting code inside a branch which is a short form, and hence the offset offerflows. Call .Simplify () on the MethodBody before instrumenting it, and call Optimize () on it when you’re done. Here, the «bug» is gone.