Why do I get InvalidCastException when I try to get Instruction from MethodBody? (Mono.Cecil)

73 views Asked by At

I'm using this code:

private static void DoWork(ref MethodDefinition methodDefinition, int index)
{
    var originalInstruction = methodDefinition.Body.Instructions[index];
    // ^
    // | exception is on this line
    // |
}

I'm getting InvalidCastException every time.
methodDefinition.Body.Instructions is a Mono.Collections.Generic.Collection
How to fix it?

Stack trace: http://image.prntscr.com/image/e20e4b00143140a79497b71f0906119e.png

I tried to debug it for so long time. It seems that when I pass MethodDefinition to function - it's not null. But when first line from function is executing it's setting to null for some reason. What?..

0

There are 0 answers