Is there a way to navigate to real implementation of method behind an interface?
In Visual Studio, when you right-click a method call, you go to the implementation of that method inside a class except if you access this method through an interface: in that case you go to the interface method not to the actual implementation.
Is there a way / tips (key shortcut or anything) to access this actual implementation ? Otherwise you are stuck to add some comment just to remember where you did implement it that's really not productive and error prone !
Update: interesting answers but I'm not really satisfied because all are cumbersome. I will give a precise example:
IInterface iInterface = someObject;
iInterface.someMethod();
Actually if Visual Studio was just a little bit clever to look just one line above the method call it would see where's the real object is. And that would save me a lot of keystrokes and avoid to use "find all references" and then scan the lines with my tired eyes to see which line contain the right one :)
Solution 1:
I do the following:
1) Right click on the method and click "View call hierarchy" (or shortcut Ctrl+K, Ctrl+T)
2) Expand the "Implements x" folder which will then show you all the implementations of that method. Click on one to go there.
Relatively quick and easy. Annoyingly though there doesn't seem to be an equivalent for the interface itself.
update: as of Visual Studio 2015 update 1, right click on a method and select go to implementation. You can also map it to keyboard shortcut via Tools > Options > Environment > Keyboard and search for Edit.GoToImplementation command. The default shortcut is Ctrl+F12
. (F12
will navigate to the interface).
Solution 2:
With VS2013 one can place cursor over the method, and use Navigate To... (CTRL+,), and it will display all locations where the name is declared. Doesn't work well if different interfaces uses the same method names.
With VS2015 Update 1 there is now a new shortcut called "Go To Implementation".
Solution 3:
I created a free extension for Visual Studio 2010 and Visual Studio 2012 called Inheritance Margin to provide this specific feature, as well as give a clear indication when a method implements an interface method due to a signature match. In the current version, you can right click on any glyph to get a menu of items to navigate to.
Inheritance Margin - Visual Studio Gallery
(source: microsoft.com)
Solution 4:
Right-click then "Find All References".
This will display the line of code for all the places where the method is used including the interface declaration and implementations of the interface method. You can then click on the line to jump to the code location.