Visual Studio: How to find out if Method A can be reached from Method B?

200 views Asked by At

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.

1

There are 1 answers

1
Colin On

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.