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.

Reply via email to