Hi, Control flow on IL level is really not this simple. You have non-conditional and conditional branching opcodes and that's it. You need to check the arguments to those opcodes and attempt to construct a control flow graph from the instruction stream, which you can then proceed to try to "decompile" into high-level statements. However, in most cases, stuff like a for loop will be hard to identify as anything but a while loop, so don't expect to get the exact original code back.
For switch statements, you can look for the 'switch' instruction and/or 'forests' of conditional branching. I'm not sure if the C# compiler uses dictionaries for longer switches; you'll have to check and verify this. I recommend having a look at ICSharpCode.Decompiler[1]. You can probably lift some code from there or perhaps even use the entire library for whatever it is you're doing. Either way, you'd be reinventing quite a few wheels by writing another decompiler from scratch. Regards, Alex [1] https://github.com/icsharpcode/ILSpy/tree/master/ICSharpCode.Decompiler 2011/6/23 deedee <[email protected]>: > Hi All, > > How to differentiate between the beginning and ending of the following > by looking at each instruction in the InstructionCollection? > > while, do-while, switch (also beginning and ending of each case) if - > else and for loops > > I am doing the following for the if condition(I think will work for > for-loop too). Its working but it doesn't look like its the best way > to do this > > if( instructions[i].OpCode.Code == Code.Brtrue_S || > instructions[i].OpCode.Code == Code.Br_S) && (instructions[i].Next != > null && instructions[i].Next.OpCode.Code == Code.Nop) // if condition > brace > { > // do something > } > > Thanks, > > -- > -- > mono-cecil -- -- mono-cecil
