We help IT Professionals succeed at work.

How to find all places that call one specific method?

For a .net solution (in C#), I want to find all places that call one specific method. In visual studio, I can get this through "Find All References". But how can I acheive this in the programming?
Comment
Watch Question

Daniel Van Der WerkenIndependent Consultant
CERTIFIED EXPERT

Commented:
Control K+ Control R will do a "find symbol results".  Other than that, I don't know what you mean.

Author

Commented:
What do you suggest is in the Visual Studo.
I mean I want to write a programming to have the "find all reference" funtionality.
Jens FiedererTest Developer/Validator

Commented:
This is not, in general, possible (if unmanaged code is included - not 100% sure if we are only in managed code): when code is running you cannot always distinguish data from program, and it is possible that a user might enter data that ends up being executed (say your program let's a user type in MSIL and executes it, that could end up running ANY function!).

If you are running the program in debug mode, you can put a breakpoint on the function in question and see when it gets hit and tell by the call stack where from (and if it is YOUR function, you can add traces to show stack when it is actually called) but there might not be a way to find every call (let's say there is a branch taken only when the year is greater than 2050, that breakpoint won't hit for decades!).

Author

Commented:
I did not mean "in the running time".
I just want to write a program that has the funcationality "Find All References" (just like in the VS).
I think, as long as VS can do it, we should be able to achieve the same functionality in our own programming.

Jens FiedererTest Developer/Validator

Commented:
VS can only do a limited version of it - since it parses all the code in the solution (for intellisense and such) it can find static calls (but of course not of code outside your solution that might link your project in the future).

A lot of the features available in Visual Studio are made available using the DTE2 project - see http://msdn.microsoft.com/en-us/library/t6d9sf9k(v=VS.100).aspx

There might be easier ways to do it (I haven't attacked this specific problem), but you can use it to load a solution, go through the files, and even down to the individual code items.  This would probably be easier than writing your own C# parser to do the job (which is, of course, also possible, but quite a project).
nishant joshiTechnology Development Consultant
CERTIFIED EXPERT

Commented:
where you want to use it any idea is it your gui application.??

Author

Commented:
I want this in a separate application, it may be GUI or not.
You need to be rather clearer about just what you are trying to achieve.

Most (all?) of the responses so far appear to be interpreting your question as asking:

"How to find all references to a particular object (method, variable, etc.?) within an application, without using the features present in Visual Studio".

But perhaps you mean instead to ask something like:

"How do I code a feature, within my application, which will find all references to a specified string, within all the files (of a specified type) present within a specified directory tree?"
"i.e. how do I provide a similar feature to the 'Find all References' mechanism?"
CERTIFIED EXPERT
Top Expert 2016
Commented:
the 'Find all References' is a non-trivial functionality. especially for multi-project solutions and big project trees. if you don't want to write a c# parser you might think of running doxygen on your sources on a regular base. it will produce references for each function and put them into an xml file which surely is easier to parse than all c# sources.

Sara
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
I think he's just looking for how to implement a find algorithm that finds all instances of a string and lets you see all the results.

Author

Commented:
DansDadUK:
My question is "How to find all references to a method within an application, without using the features present in Visual Studio".

DansDadUK and TommySzalapski:
"Find all references to a method" is quite different from "Find a string".  The latter is much easier.  
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
So you just need to keep (or calculate when needed) some kind of symbol table or syntax tree that knows the type of each symbol so you can search for the string and make sure it is connected to the correct type. You would need to also watch for member methods that call the method since they won't have an explicit object connected with a . or -> or whatever.

Is that more like what you are going for?

Author

Commented:
What I'm looking for is:
   In an application, a method may be called in many places. I just want to get all those places (file, class, and method) that call this method.

   In visual studio, we can use "Find All References" to get this information.
   But I need to find the references to thousands of methods, so I cannot use visual studio. I have to create my own code to implement this.
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Then my last post should apply. If you don't control the IDE, you'll just have to write a parser for the code files.
CERTIFIED EXPERT

Commented:
As sarabande already suggested, try the Doxygen tool -- see the http://www.doxygen.org/ (GNU General Public License) that can be downloaded also in binary form from http://www.stack.nl/~dimitri/doxygen/download.html#latestsrc.

You can install also the in the doc mentioned Dot tool that allows Doxygen to generate images (graphs) of various dependencies.  However, having that much files, you will be satisfied with having index pages and various textual refrences.

Author

Commented:
I'm trying to use Doxygen. Will get back once I get the result.
Jens FiedererTest Developer/Validator

Commented:
That the question has NOT been answered is inconceivable.

But which answer was most useful to the author (or WOULD be useful to the author if the quest was still being pursued) only the author can answer.

I suspect the author was hoping for some nonexistent answer that did not involve doing any work - who wouldn't?
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
sarabande's doxygen suggestion http:#37137810 and my suggestion about the symbol table http:#37140120 would prove to be the easiest way to implement what the asker wants.

I've not used Doxygen, but I imagine based on her post that there is a way to tie the entries in the XML tree to the lines of code where those methods were called. If that is not the case, then he's stuck parsing the raw C# code.
CERTIFIED EXPERT
Top Expert 2016

Commented:
it is many work to setup a doxygen run on a regular base and evaluate the doxygen output for own representations. i can understand that the asker didn't proceed on that as it probably would exceed the amount of efforts he/she wanted to spend for that.

beside of that evaluating the results of another tool for own purposes is a valid way to do things like those the questioner has asked for. i did those kind of evaluations in nearly any major project.

Sara
Jens FiedererTest Developer/Validator

Commented:
An amusing name for a product, since "doxy" means prostitute, mistress or love slave.

I guess I am archaic!

http://en.wikipedia.org/wiki/Doxy

Author

Commented:
Yeah, the parser or doxy may be used to achieve what I want, but the effort may be big, which is not what I expected.
Thank you very much for all your replies.
I will mark parser and doxy as the answer.
Hope some time in the near future, there is much easier to get the "all reference" thing from the code.

Author

Commented:
It seems thare is no "easy" solution to this finding "all reference" issue. The solutions suggested by sarabande and Tommy may be used to achieve this, authough much effort my be needed.
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.