Hey Kristian, That's actually a good question. Cecil itself only provides you with the list of instructions, and has no builtin way to achieve what you want.
It's not a trivial problem. Imagine if there's a ?: operator in mix :) If you want to make complex manipulation, you can use ILSpy's decompiler engine to give you the IL range for expressions. Robert's solution is much simpler. Simply redirect the calls to something that you control or generate yourself. Jb On Thu, Mar 10, 2016 at 10:06 AM, Robert Baumann <[email protected]> wrote: > It's true that there will be additional instructions generated before the > actual Console.WriteLine() takes place in this scenario. However, assuming > that you don't mind that those additional instructions get executed and you > just want to control whether there is output to the console, you could > simply focus on the single instruction call to Console.WriteLine() itself. I > recommend using Reflector in IL code view to see the call pattern. > > Another way to potentially think about this problem is to use code to > replace calls to Console.WriteLine() with Debug.WriteLine() or > <YourObjectHere>.WriteLine() by switching the method reference that is > called, but you should be sure that all 19 method signatures of > Console.WriteLine() work for this case. > > On Thu, Mar 10, 2016 at 5:48 AM, kristian mortensen > <[email protected]> wrote: >> >> Hi there >> >> >> Please have me excused if this is something that seems trivial to do for >> you guys but I'm new to using CECIL and have only done rudimental stuff. >> >> >> I'm trying to inject a "if( staticDebugOn ) Console.WriteLine" before each >> call to Console.WriteLine in a set of assemblies. So far i managed to create >> the "if( staticDebugOn" however what I dont understand how to find the >> proper line to inset the instructions. >> >> Do i need to create and analyze a the stack of the CIL Instructions of the >> method that im changing and then inject the instructions when the stack has >> the same number of elememts as when after the Console.Write has beeen called >> ? >> >> // Creating field... >> TypeReference bool_type = module.Import( typeof(bool) ); >> fiDef = new >> FieldDefinition("staticDebugOn",FieldAttributes.Static|FieldAttributes.Public, >> bool_type); >> type.Fields.Add (fiDef); >> >> // creating instructions.... >> var i1 = Instruction.Create( OpCodes.Ldsfld, fiDef ); >> var i2 = Instruction.Create( OpCodes.Brfalse, i1 ); >> >> I guess that if Console.WriteLine is called with just a string like : >> Console.WriteLine( "helloworld" ); its easy to find the right location with >> the instructions to inject i1 and i2 . that must be before the instruction >> to insert the strig "helloworld" on the stack, right ? >> >> However, a lot of cases involves a more advanced set of instructions. >> Consider this piece of code: >> >> Console.WriteLine( "My Int was" + myint ); >> >> Or >> >> Console.WriteLine( >> GetCheckSumAsStr(somefloat+3.0f)+String.Format("{0}",(""+(myint++)) ); >> >> >> That will naturally resort in alot of instructions and values being pushed >> and poppen on top of the stack before actually calling Console.WriteLine. >> >> So to sum up the question, anyone got a clue to how i can figure out where >> to insert the instruction for any case involving Console.WriteLine( >> whatever.... ) >> >> >> Cheers! >> >> -- >> -- >> -- >> mono-cecil >> --- >> You received this message because you are subscribed to the Google Groups >> "mono-cecil" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. > > > -- > -- > -- > mono-cecil > --- > You received this message because you are subscribed to the Google Groups > "mono-cecil" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- -- -- mono-cecil --- You received this message because you are subscribed to the Google Groups "mono-cecil" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
