Coder Perfect

Is there a way to get to the actual implementation of a method that is hidden behind an interface?

Problem

When you right-click a method call in Visual Studio, you go to the class implementation of that method, unless you access the method through an interface, in which case you go to the interface method instead of the real implementation.

Is there a method to get to this actual implementation / any advice (key shortcuts or anything)? Otherwise, you’ll be forced to write a remark merely to remember where you put it, which is both inefficient and error-prone!

Update: fascinating responses, but none of them satisfy me because they are all cumbersome. Let me give you an example:

IInterface iInterface = someObject;                        
iInterface.someMethod();

Actually, if Visual Studio was just a little bit smarter, it could see where the real object is just one line above the method call. And it would save me a lot of keystrokes and prevent me from having to use “find all references” and then scanning the lines with my tired eyes to discover which line contains the correct one:)

Asked by user310291

Solution #1

Here’s what I do:

1) Right-click the method and select “View call hierarchy” (or Ctrl+K, Ctrl+T) from the context menu.

2) Expand the “Implements x” folder, which will display all of the method’s implementations. To navigate to a specific location, simply click on it.

It’s a simple and quick process. Unfortunately, there does not appear to be an equivalent for the interface.

Update: In Visual Studio 2015 Update 1, right-click a method and choose Go to Implementation. You may also use Tools > Options > Environment > Keyboard to search for Edit and assign it to a keyboard shortcut. The command GoToImplementation is used to navigate to a certain implementation. Ctrl+F12 is the default shortcut. (To get to the UI, press F12).

Answered by MajorRefactoring

Solution #2

With VS2013, you may hover the cursor over the method and select Navigate To… (CTRL+,) to see all the places where the name is declared. When different interfaces use the same method names, it doesn’t work properly.

There is a new shortcut called “Go To Implementation” in VS2015 Update 1.

Answered by Rolf Kristensen

Solution #3

To enable this capability, I wrote the Inheritance Margin free extension for Visual Studio 2010 and Visual Studio 2012, which also provides a clear signal when a method implements an interface method due to a signature match. In the current version, you may right-click any glyph to bring up a menu of options.

Visual Studio Gallery – Inheritance Margin

(source: microsoft.com)

Answered by Sam Harwell

Solution #4

Select “Find All References” from the context menu.

This will show the line of code for all of the places where the method is used, including the interface declaration and implementations. Then, by clicking on the line, you can jump to the code place.

Answered by Phil Carson

Solution #5

Edit is an option. Ctrl + F12 will take you to Implementation.

It will direct you to implementation in the same way as F12 would lead you to non-interface methods.

Answered by Akbar Badhusha

Post is based on https://stackoverflow.com/questions/4662784/is-there-a-way-to-navigate-to-real-implementation-of-method-behind-an-interface