IL Invalid program, can't understand why

I am emitting IL, here is my code:

mgen.Emit(OpCodes.Ldc_I4,0);
mgen.Emit(OpCodes.Newarr, typeof(object));
mgen.Emit(OpCodes.Stloc_1); // THIS SHOULD                       


mgen.Emit(OpCodes.Ldloc_1); // MATCH THIS ONE
mgen.Emit(OpCodes.Callvirt, typeof(IInternalFactory).GetMethod("Create"));
mgen.Emit(OpCodes.Castclass, method.ReturnType);
mgen.Emit(OpCodes.Ret);

This work in progress, I created an array, and now I'm ready to do something with it, so I decided to save it (Stloc_1), and then put it back on the stack (Ldloc_1) as soon as I made this instruction, that IL is signaled as invalid, but if I understood correctly, this instruction should leave the stack unmodified. Without these two commands, IL works fine. Therefore, I can not understand why pop with a subsequent click does not work.

+3
source share
1 answer

, , ILGenerator.DeclareLocal. , , , JIT .

+5

All Articles