5

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.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • Resharper has this. It wills show you all usages of a method. – Oded Jan 09 '13 at 17:50
  • 3
    Right-click on the method -> "View Call Hierarchy"? – Brendan Hannemann Jan 09 '13 at 17:52
  • 2
    Of if you want to roll your own: http://stackoverflow.com/a/5741770/16959 this is an extremely well researched answer on this topic – Jason Sperske Jan 09 '13 at 17:57
  • Oded and unicron - Thanks for the replies, but I already know about the call hierarchy. However, unless I've missed something, they only allow you to see methods called by the current method. You then need to go through each called method and do the same thing on each of them. You also need to keep a track of all of this as you go along. This becomes very painful and error-prone very quickly. I'm looking for a way of generating a full hierarchy automatically. If either R# or VS's feature can do this, then please tell me how, as I didn't think they could. – Avrohom Yisroel Jan 09 '13 at 18:09
  • Jason - thanks for the link, that looks fantastic. I haven't tried it yet, as it looks like it will need some serious study, but if it is as good as it looks, it should do exactly what I want. Thanks again. – Avrohom Yisroel Jan 09 '13 at 18:12

1 Answers1

10

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.

Sebastian K
  • 6,235
  • 1
  • 43
  • 67
  • Sebastian - thanks for the reply, but if you read my reply to Oded and unicron above, you'll see why this feature isn't powerful enough for what I need. Testing is an interesting approach, but it would be too much work to do now on an existing (and very large) class, and too hard to analyse to see what called what. – Avrohom Yisroel Jan 09 '13 at 18:16
  • 1
    View Call Hierarchy actually shows you kind of double tree - or tree and roots :) So that you can see all methods calling given method as well all methods called by given method. – Sebastian K Jan 09 '13 at 18:53
  • Also "Code Analysis" if available in your edition of VS studio may be useful, although it may not give you everything you are looking for. It is described here: http://stackoverflow.com/questions/2020297/visual-studio-2008-c-sharp-how-to-find-dead-code-in-a-project – Sebastian K Jan 09 '13 at 18:54
  • Sebastian - Thanks for the tip, but that still only shows one level at a time. Building up a picture of the full tree would still take a long time and be painfully slow, not to mention error-prone. I have VS2010 Pro, but can't see a Code Analysis option when I right-click a project. Do you know how to get at it, or is it not in my version of VS? (edit...) Ah, just discovered it's only in premium and ultimate. Shame, thanks anyway – Avrohom Yisroel Jan 09 '13 at 19:53
  • Yeah, I wish it was easier too. If you have a ReSharper (or want to get a trial version). You could also try to run "Find Code Issues" on your solution, and then look for "Unused ..." issues. As people pointed out though, it will report items are used even if they are just used in tests or registered with DI container. http://stackoverflow.com/questions/245963/find-unused-code – Sebastian K Jan 09 '13 at 22:03
  • Hello again Sebastian. I have R#, and use all the time. The code issues is a good feature, but here I'm looking for a lot more than just unused code, I need to see actual usage of all methods. At the moment, it looks like I'm going to have to write my own code to parse the class :( Thanks anyway – Avrohom Yisroel Jan 13 '13 at 15:52
  • 1
    @AvrohomYisroel: If you still working on this one, I recently stumbled upon http://stackoverflow.com/questions/5490025/c-sharp-reflection-and-find-all-references this would use reflecion on compiled DLL approach – Sebastian K Jan 24 '13 at 01:29
  • Hello. Thanks for that link, some interesting ideas there. I ended up writing my own code to parse a .cs file and working out what methods are called by what. Not perfect, but working pretty well. However, there are some ideas there I woudl like to investigate as they might pove to be a better way still. Thanks again. – Avrohom Yisroel Jan 24 '13 at 23:23