如何判断一个方法中调用了哪些方法?
我想列出从特定方法调用的所有方法。例如,如果我有以下代码:
public void test1() {
test2();
test3();
}
列表应包含 test2() 和 test3()。如果可以列出同一类的方法以及另一个类的方法,那就太好了。
另外,我想找到一种方法来检测方法使用了哪些字段:
public class A {
private String test1 = "";
private String test2 = "";
public void test() {
Console.WriteLine(test1);
}
}
因此应该列出 test1.
我使用 Mono.Cecil 进行了尝试,但不幸的是我找不到很多有关该项目的文档。那么有人知道该怎么做吗?
编辑:我想用 Mono.Cecil 来做,因为通过它的 API 我可以直接在我的应用程序中使用结果。如果我使用 Visual Studio 或类似工具中的内置工具,则很难进一步处理结果。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有真正与塞西尔合作过,但 HowTo 页面显示了如何枚举类型,你的问题似乎只是需要循环遍历您之后的指令:调用和加载字段。此示例代码似乎可以处理您提到的情况,但可能还有更多情况,您也应该检查其他 Call 指令。如果您使其递归,请确保跟踪已经检查过的方法。
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.
仅使用 C# 中的反射 API 无法完成此操作。实际上,您需要解析原始源代码,这可能不是您正在寻找的解决方案。但例如,这就是 Visual Studio 获取此类信息以进行重构的方式。
您可能会在某个地方分析 IL - 沿着 Reflector 所做的事情,但我认为这将是一项艰巨的工作。
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.
如果您想付费,可以使用.NET Reflector工具。您还可以查看.NET 方法依赖项不过,这会变得很棘手,因为您将进入 IL。第三种可能是使用VS中的宏引擎,它确实有一个分析代码的工具,
CodeElement
,但我不确定它是否可以做依赖项。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.