ILGenerator emits Break statement when storing items in an array

I generate dynamic types using ILGenerator.Emit. I create a method body that will store the types of the arguments of the method in an array. To actually store the elements in an array, I iterate over the parameters of this method and create the necessary IL to store the elements. In the second iteration, the Break command appears after the Stelem.ref instruction (L_003d below) . This always happens in the second iteration, and I cannot understand why. Here is the code:

        ilGenerator.Emit(OpCodes.Ldc_I4, exampleMethod.GetParameters().Length);
        ilGenerator.Emit(OpCodes.Newarr, typeof(Type));
        ilGenerator.Emit(OpCodes.Stloc, typeArray);

        for (int idx = 0; idx < exampleMethod.GetParameters().Length; idx++)
        {
            ilGenerator.Emit(OpCodes.Ldloc, typeArray);
            ilGenerator.Emit(OpCodes.Ldc_I4, idx);
            ilGenerator.Emit(OpCodes.Ldarg, idx + 1);
            ilGenerator.Emit(OpCodes.Box, typeof(int));
            ilGenerator.EmitCall(OpCodes.Callvirt, typeof(object).GetMethod("GetType"), null);
            ilGenerator.Emit(OpCodes.Stelem_Ref, idx);//second iteration causes a break to be output in the IL
        }

        ilGenerator.Emit(OpCodes.Ret);

and the conclusion of IL is here

.method public virtual instance int32 Add3(int32, int32, int32) cil managed
 {
.maxstack 3
.locals init (
    [0] class [mscorlib]System.Type[] typeArray)
L_0000: ldc.i4 3
L_0005: newarr [mscorlib]System.Type
L_000a: stloc.0 
L_000b: ldloc.0 
L_000c: ldc.i4 0
L_0011: ldarg A_0
L_0015: nop 
L_0016: nop 
L_0017: box int32
L_001c: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
L_0021: stelem.ref 
L_0022: nop 
L_0023: nop 
L_0024: nop 
L_0025: nop 
L_0026: ldloc.0 
L_0027: ldc.i4 1
L_002c: ldarg A_1
L_0030: nop 
L_0031: nop 
L_0032: box int32
L_0037: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
L_003c: stelem.ref 
**L_003d: break** 
L_003e: nop 
L_003f: nop 
L_0040: nop 
L_0041: ldloc.0 
L_0042: ldc.i4 2
L_0047: ldarg A_2
L_004b: nop 
L_004c: nop 
L_004d: box int32
L_0052: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
L_0057: stelem.ref 
L_0058: ldarg.0 
L_0059: nop 
L_005a: nop 
L_005b: nop 
L_005c: ret 
}

Any pointers or suggestions are welcome.

Many thanks

Dermot

+3
source share
1 answer

0x01, , , idx, emle stelem.ref. , ldarg.0 ( idx 2).

.

+5

Source: https://habr.com/ru/post/1772803/


All Articles