Rafi
Rafi

Reputation: 2484

Find All References To Child Method

I want to find all calls to DateTime.ToString references in my assembly. In Visual Studio you can "Find All References" by right clicking on ToString; however, that returns all references to ToString for all classes and not just DateTime.

A regular search for DateTime.ToString won't work because of the following example:

DateTime myDate = new DateTime();
myDate.ToString();

Any suggestions?

Upvotes: 21

Views: 2687

Answers (5)

user1325179
user1325179

Reputation: 2085

In Visual Studio 2019, right-click on the desired method name, and then click "View Call Hierarchy." A "Call Hierarchy" window appears at the bottom (by default) of Visual Studio. In the left pane, you'll see a tree representation of the projects that call the method. Within each project, the first item is "Calls To [MethodName]." Expand this first item for a list of methods. Clicking each method will change the right pane of the "Call Hierarchy" window to show the lines where the method is called. Double-click these lines to go to them and address them individually.

Upvotes: 3

Jacob
Jacob

Reputation: 667

In VS2015 when you try to Find References for override method, you have them grouped by type in
Results like this: Find Symbol Results

enter image description here

Upvotes: 9

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

In visual studio, AFAIK it is not possible, though resharper can.

In resharper, Find Advanced Usages window you can filter out which type you're looking for. You can specify less derived or more derived.

So in this case, you can filter out only DateTime, or all ValueType or System.Object.

enter image description here


In this case I guess visual studio can't help, still I'd like to share some tricks.

For example: If you want to find all references of PropertyChanged event of your Person class assuming person implements INotifyPropertyChanged, You are going to get tons of references as there are numerous classes which implements INotifyPropertyChanged.

What you can do about it is, just go to Person class and comment out the implementation of INotifyPropertyChanged interface. Then Find all references in visual studio will tell you only the references of Person class. Not all types.

At least I hope this trick can help in some other place if not now.

Upvotes: 1

alex.b
alex.b

Reputation: 4567

You should use ReSharper for that.

Steps:

  1. Download and install ReSharper (trial version will work for you)
  2. Open your project/solution in Visual Studio
  3. Find usage of ToString(), like myDate.ToString();
  4. Right click on ToString token
  5. Select Find Usages Advanced ... menu item
  6. Tick the appropriate boxes in window as on screenshot below and click Find
  7. PROFIT

enter image description here

Upvotes: 17

gturri
gturri

Reputation: 14599

The commercial tool NDepends is really good for that: it lets you find what assemblies / classes / methods use a particular one.

It may not be cheap, but if you have a large code base -especialy with several assemblies- it can be very effective!

Upvotes: 0

Related Questions