Navigate To Actual Implementation Of Method Of Interface In Visual Studio

Background

These days, we develop most web applications following either SOLID principle or Onion architecture. We use interfaces in our application in both ways and it’s good practice to use interface with communication with UI, which means controller in MVC so that the application may be loosely coupled.

We develop an MVC application which has employee registration functionality. In order to achieve this we create both class and interface for employee and call method as below.

  1. IEmployee employee = new Employee();  
  2. employee.SaveEmployee("John"new DateTime(1988, 11, 29));  
Now we want to navigate to actual implementation of method SaveEmployee() then we either press F12 key or “Go to Definition” option by right clicking on the mouse  but it navigates in interface where method signature is defined instead of definition. So, the problem is how to navigate to actual implementation of method of interface in Visual Studio.

Solution

We follow the following steps to navigate to actual implementation of method of interface. These are:

Step 1: Right click on method name and click on “View Call Hierarchy” option from the pane as shown in figure 1.

View Call Hierarchy Option
                           Figure 1: View Call Hierarchy Option

We can also use short key Ctrl+K, Ctrl+T for same.

Step 2: After clicked on “View Call hierarchy” option, a Call Hierarchy window shows at bottom in Visual studio.

This window has 3 options which are as per figure 2.

Call Hierarchy Window
                                                       Figure 2: Call Hierarchy Window 
  1. Calls To Method Name:

    This option shows where the method is called. In our example SaveEmployee method is called in Main method.

  2. Calls From Method Name:

    This option shows from which location this method is called. In our example, it is not supported to due to interface.

  3. Implements Method Name:

    It’s our solution. It’s exactly the same as what we are  looking at here. It shows actual implementation of method. There is method signature, we need to double click on this and it navigates to actual implementation of method. In our example we are using IEmployee interface instance which created for Employee class object so this call hierarchy navigates to SaveEmployee method in Emplyee class where actual implementation defined.
Read more articles on Visual Studio: