Is it possible with reflection to find all method calls from one class to another?
I want to find开发者_如何学C all the method calls in one class which are made on another class.
For example:
If class1 calls class2.Foo() but not class2.Bar() then I want to know about it.
Almost like an analysis of coupling. Is this possible with reflection?
Nope, reflection is all about types, not code. You can find out anything you want with System.Reflection about what a type looks like: fields, properties, events, and methods. But method calls are encoded in CIL. Reflection stops there, and all you got is MethodInfo.GetMethodBody().
That didn't stop some people; you can actually interpret the CIL handed to you by the method. A shining light there is Lutz Roeder and his awesome .NET Reflector tool. Ninety percent of what I know about how the .NET framework actually works, and how I can advantage of it myself, was handed on a silver platter. It is very awesome; give the guy a medal. And MSFT following up on that with the Reference Source.
This is possible, as it is done by several IDEs (at least for Java). In any case, you're likely to have a partially erroneous analysis. It is very difficult to determine at compile-time which methods are called in presence of method overrides in derived classes.
No, it is not possible with reflection. You need static code analysis to compute coupling metrics.
You don't say if this is in code or as a utility. In code you would need to inspect the CIL of a class.
For utilities you could try .NET Reflector or NCover (I am not sure if I have the correct name). .NET Reflector has a plug in interface so you may be able to do something with that.
精彩评论