1
Vote

Exception when switch Instruction is emitted

description

Hi there,

I found an issue with the ILGenerator when you want to emit "switch" instructions.

The Problem lies in the function:
public void Emit(OperationCode opcode, object value) {
in the ILGenerator.cs.

When a switch instruction is emitted, the value object is of the type uint[]. This case is not handled.

I fixed it by iterating through the array and create ILGeneratorLabels from it. Here is the patch I used at the end of the function (before the Exception is thrown).
    public void Emit(OperationCode opcode, object value) {
[...]
      switch (System.Convert.GetTypeCode(value)) {
[...]
        default:
          var fieldReference = value as IFieldReference;
          if (fieldReference != null) {
            this.Emit(opcode, fieldReference);
            break;
          }
[...]

          // start fix
          uint[] offsets = value as uint[];
          if (offsets != null) {
              ILGeneratorLabel[] offsetLabels = new ILGeneratorLabel[offsets.Length];
              for(int i = 0; i < offsets.Length; i++) {
                  ILGeneratorLabel temp = new ILGeneratorLabel();
                  temp.Offset = offsets[i];
                  offsetLabels[i] = temp;
              }
              this.Emit(opcode, offsetLabels);
              break;
          }
          // end fix

          throw new InvalidOperationException();
      }
    }
Regards

sqall

comments