Question

I think I might be missing something important, but I can't seem to figure out how to construct a conditional statement in IL with dynamic method. I've only dabbled lightly in it before, but I need to extend some code now.

Is there some documentation somewhere that I haven't found (apart from the CLI documentation), or does someone have some sample code? That would be fantastic.

Cheers,

Was it helpful?

Solution

Depending on your exact code, the various branch instructions are your friend.

Here is

if(memory[pointer] > 0) goto IL_0002;

in IL:

IL_001f:  ldsfld     uint8[] BFHelloWorldCSharp.Program::memory
IL_0024:  ldsfld     int16 BFHelloWorldCSharp.Program::pointer
IL_0029:  ldelem.u1
IL_002a:  ldc.i4.0
IL_002b:  bgt      IL_0002

You basically push the values you want to compare onto the stack and then call bgt to jump where you need to.

You can check the OpCodes Class for a quick overview of IL Commands, for example brtrue/brfalse or beq.

I would also recommend writing the if command in C#, compiling it, and using ILDASM or Reflector to look at the generated IL.

OTHER TIPS

Here's how it goes:

Define a label, e.g.:

var skipProperty = il.DefineLabel();

call your condition:

il.Emit(OpCodes.Brtrue, skipProperty);

in the place where you want it to skip to (e.g. the end of the if):

il.MarkLabel(skipProperty);

So you create a label (you need to do it first so the reference exists, you call 'mark' later to place the label in the actual spot in code you want it). the OpCodes.Brtrue is just one of many conditional operations listed in the link text article (thanks Michael

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top