1

In Visual Studio 2019 using C#, is it possible to check if Method A can be reached from Method B? Ideally, I'd like to capture the whole graph/stack trace. Note that I'm not necessarily interested in the Find All References feature since it seems to be finding direct references to a method. There could be multiple layers of indirection between Method A and Method B.

user246392
  • 2,661
  • 11
  • 54
  • 96
  • You can use ReSahrper for this. Alternativly VS has the calling-hierarchy-feature for that. Right-click your member and select calling-hierarchy. – MakePeaceGreatAgain Dec 04 '20 at 22:33
  • I haven't looked at ReSharper, but the call hierarchy feature seems to require manual work since I have to choose the next method at each level of the hierarchy. I was hoping to input/select the starting and end methods and see if there is a link between them. – user246392 Dec 04 '20 at 22:42
  • I doubt there is some automatic way for that. – MakePeaceGreatAgain Dec 04 '20 at 22:47
  • 2
    It's not clear if you're talking about the VS GUI or actually doing it in code. – Blindy Dec 07 '20 at 18:03
  • I want to do this in the GUI similar to, say, how code maps are generated. – user246392 Dec 08 '20 at 01:50
  • Maybe you could use call stack window with breakpoints or F11 step by step debugging, and it could help you view the current call stack in visual studio debugger. – 大陸北方網友 Dec 08 '20 at 05:40
  • There is a lot of branching in the code depending on the state of the data. Debugging it won't be easy. – user246392 Dec 08 '20 at 15:34
  • You probably want the Enterprise edition of VS with the Architecture Tools. https://devblogs.microsoft.com/devops/wp-content/uploads/sites/6/2014/10/2844.image_thumb.png – Jeremy Thompson Dec 14 '20 at 02:59
  • Are the methods in different classes? Different projects? – asaf92 Dec 14 '20 at 14:44
  • @asaf92 Yes, they are in different classes and may be in different projects as well, but all projects are loaded under the same solution. – user246392 Dec 14 '20 at 21:22

1 Answers1

0

It's just a feature of the IDE and the nature of complied languages.

Simply add the namespace to the code file containing "Method B".

Then try to call "Method B", e.g. MyStaticClass.MethodB().

If the code doesn't give you an error (indicated by a red squiggly underline and compile error), then the method is reachable.

Programmatically, you can do it through something called "reflection", in the following steps:

  1. Check that "Method B"'s assembly is loaded.
  2. Check that Method B's parent class is public.
  3. Check that Method B is public.

This seems outside the intention of your answer, so I'll skip the code samples, but I just wanted to point out it's possible both with the IDE and with code.

Colin
  • 4,025
  • 21
  • 40