If you really just want to convert true
to 1
and false
to 0
, you can use the fact that types smaller than 4 bytes are expanded to 4 bytes on the stack. This means you don't have to do anything and just treat the bool
as an int
.
If you actually want a ternary operator, you can use the pair of methods DefineLabel()
and MarkLabel()
:
var trueLabel = il.DefineLabel();
var endLabel = il.DefineLabel();
il.Emit(OpCodes.Brtrue, trueLabel);
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Br, endLabel);
il.MarkLabel(trueLabel);
il.Emit(OpCodes.Ldc_I4_1);
il.MarkLabel(endLabel);