views:

28

answers:

3

I want to find 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?

A: 

This is possible, as it is done by several IDEs (at least for Java). In any case, you're likely to have a partially erreneous 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.

André Caron
This is true. It is probably a little too much work at this stage. Thanks
James Hulse
+1  A: 

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, methods. But method calls are encoded in IL. Reflection stops there, all you got is MethodInfo.GetMethodBody().

That didn't stop some people, you can actually interpret the IL handed to you by the method. Shining light there is Lutz Roeder and his awesome 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. Very awesome, give the guy a medal. And MSFT following up on that with the Reference Source.

Hans Passant
Hmm, that is what I suspected. I wonder how hard the IL translation would be. Probably more effort than it is worth at this stage for me I imagine. I have seen a few properties of Assembly which seem to be about linked assemblies which may give hints as to other assemblies which have been called within the assembly that you are analysing though.
James Hulse
It's do-able, but doing a better job than what's already available is indeed hard. What's available is *that* good. Getting referenced assemblies is easy, just use Assembly.GetReferencedAssemblies().
Hans Passant
A: 

you dont say if this is in code or as a utility. In code you would need to inspect the IL of a class.

For utilities you could try reflector or ncover (not sure if i have the correct name). Reflector has a plug in interface so you may be able to do something with that

pm100
It would be as a utility, I just stumbled upon **NDepend** and it seems like it will be able to do what I need.
James Hulse
thats the tool i meant
pm100