← Back to articles

Bug in Mono.Cecil

Mono.Cecil kept producing broken assemblies with branches jumping out of the method — until Jb Evain explained the magic: call Body.Simplify() before modifying and Body.Optimize() after. Not a bug after all.

Tedd

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.

image

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.

Written by Tedd · Oslo, Norway

Follow the journal via RSS