如何确定方法调用在一个方法? [英] How to determine which methods are called in a method?

查看:98
本文介绍了如何确定方法调用在一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出所有正在从一个特定的方法调用的方法。例如。如果我有以下的code:

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();
}

该列表应包含的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我想找到一种方法来检测哪些字段使用的方法:

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);
   }

}

因此​​,应列出测试1。

Should therefore list test1.

这个我试过用Mono.Cecil能做到,但不幸的是我没有找到很多关于该项目的文档。因此,没有任何人知道该怎么做?

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?

编辑:我想和Mono.Cecil能做到这一点,因为在它的API,我可以直接使用的结果,我的应用程序。如果我使用内置在Visual Studio或类似的工具,这是相当困难的furhter处理结果。

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.

推荐答案

我还没有真正曾与塞西尔但方法文档页面显示了如何枚举类型,你的问题似乎只需要遍历为那些你后指令:CALL和加载现场。此示例code似乎来处理你提到的案件,但可能有更多的东西,你应该检查其他调用指令了。如果你把它递归确保你跟踪你已经检查的方法。

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 => x.Name == "A");
    var method = type.Methods.First(x => x.Name == "test");

    PrintMethods(method);
    PrintFields(method);

    Console.ReadLine();
}

public static void PrintMethods(MethodDefinition method)
{
    Console.WriteLine(method.Name);
    foreach (var instruction in method.Body.Instructions)
    {
        if (instruction.OpCode == OpCodes.Call)
        {
            MethodReference methodCall = instruction.Operand as MethodReference;
            if(methodCall != null)
                Console.WriteLine("\t" + methodCall.Name);
        }
    }
}


public static void PrintFields(MethodDefinition method)
{
    Console.WriteLine(method.Name);
    foreach (var instruction in method.Body.Instructions)
    {
        if (instruction.OpCode == OpCodes.Ldfld)
        {
            FieldReference field = instruction.Operand as FieldReference;
            if (field != null)
                Console.WriteLine("\t" + field.Name);
        }
    }
}

这篇关于如何确定方法调用在一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆