How to determine which methods are called in a method?

By : Biwier
Source: Stackoverflow.com
Question!

I'd like to list all the methods that are called from a specific method. E.g. if I have the following code:

public void test1() {

   test2();


   test3();
}

The list should contain test2() and test3(). It would be great if methods of the same class but also methods of another class could be listed.

Additionaly I'd like to find a way to detect which fields are used of a method:

public class A {

   private String test1 = "";
   private String test2 = "";

   public void test() {
      Console.WriteLine(test1);
   }

}

Should therefore list test1.

I tried this using Mono.Cecil, but unfortunately I couldn't find lot of documentation about the project. So does anybody know how to do that?

Edit: I'd like to do it with Mono.Cecil because over its API I can directly use the results in my application. If I use built in tools in Visual Studio or similar, it's quite difficult to furhter process the results.

By : Biwier


Answers

This can't be done simply using the reflection API within C#. Really you would need to parse the original source code which is probably not the kind of solution you're looking for. But for example this is how Visual Studio gets this kind of info to do refactoring.

You might get somewhere analysing the IL - along the lines of what Reflector does but that would be a huge piece of work I think.



you can use .NET Reflector tool if you want to pay. you could also take a look at this .NET Method Dependencies it gets tricky though, as you're going to be going into the IL. A third possible would be to use the macro engine in VS, it does have a facility to analyze code,CodeElement, I'm not sure if it can do dependencies though.

By : jasper


I haven't really worked with Cecil but the HowTo page shows how to enumerate the types, your problem only seems to require looping over the instructions for the ones your after: Call and Load Field. This sample code seems to handle the cases you mentioned but there may be more to it, you should probably check the other Call instructions too. If you make it recursive make sure you keep track of the methods you've already checked.

static void Main(string[] args)
{
    var module = ModuleDefinition.ReadModule("CecilTest.exe");

    var type = module.Types.First(x =
By : Kris


This video can help you solving your question :)
By: admin