Load the assembly:

 AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(@"C:\...\Assembly.
dll"); 
 ModuleDefinition mainMod = asm.MainModule; 

Find the MethodDefinition for the method you're interested in:

 TypeDefinition classContainingYourMethod = mainMod.Types.FirstOrDefault (x 
=> x.Name == "ClassName"); // this is LINQ
 MethodDefinition yourMethod = classContainingYourMethod.Methods.
FirstOrDefault (x => x.Name == "YourMethodsName"); //if your method is void 
ShowTooltip(), just type in "ShowTooltip"

Now parse all Classes (types) in your assembly, parse all their Methods, 
parse all their Instructions, parse all their Operands and see if it's 
referencing yourMethod.

         foreach (TypeDefinition td in mainMod.Types)
            foreach (MethodDefinition md in td.Methods)
                if (md.HasBody)
                    foreach (Instruction inst in md.Body.Instructions)
                        if (inst.Operand == yourMethod)
                            System.Console.WriteLine ("your method is being 
called here"); 

*> how do i verify the parameters that are passed?*
Parameters are pushed onto the evaluation stack in the instructions that 
are preceding the call.

*>when finding it, i'd like to verify it was called correctly.*
Don't understand the question.

On Tuesday, November 25, 2014 11:13:49 PM UTC+1, Lior Tal wrote:
>
> Hello,
>
> I'd like to write a rule that given a method definition, looks through a 
> given assembly and checks whether this method has been invoked.
>
> Also, when finding it, i'd like to verify it was called correctly.
>
> I am pretty sure this is fairly easy to achieve with Mono.Cecil, are there 
> any good examples of code that does something similar online that i can 
> have a look at ?
>
> If not, conceptually what is the process here?
>
> Can i scan all the methods of the input assembly and look for a Call 
> instruction with the method definition i am interested in? how do i verify 
> the parameters that are passed?
>
> Any help will be greatly appreciated.
>
> Thanks
> Lior
>

-- 
-- 
--
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