Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find what methods are called from a C# class method - NOT at run time

I have a C# class that has far too much code in, and I want to refactor it. What I would like to do is start with all the public methods, and build a tree for each one, showing which other methods in the class are called from it, and then which are called from the child one, and so on.

This will enable me to see which private methods belong solely to one public method, which are shared and so on.

Note that I DON'T want to do this at run time, I want to be able to look at a class, either directly at the .cs file, or using reflection on the compiled DLL.

I know I can use reflection on the compiled DLL to get the methods, but I can't find any way of finding out which methods are called by other methods in the class.

Anyone any ideas? Again, this is NOT a run time issue, it's purely to build a reusable utility to help refactor an oversized class. There are quite a few in the solution I'm working on, so the code woudl be used over and over again.

like image 607
Avrohom Yisroel Avatar asked Jan 09 '13 17:01

Avrohom Yisroel


1 Answers

Visual Studio 2010 has action "View Call Hierarchy" where you can see all places in your solution where your code is invoked.

From my experience this static analysis may be somewhat lacking, for example method could be called dynamically using Reflection, through Data Binding, through Dependency Injection Container, etc.

Also, that may be bit off topic, and not applicable in your case, but I find a good way to find dead code for component is to have a suite of integration tests. Then you can run code coverage, and visually see what paths of code are never executed.

like image 110
Sebastian K Avatar answered Sep 22 '22 22:09

Sebastian K