How do you find all implementations of an interface? - c#

Suppose you have an interface defined in C#. What is the easiest method to find all classes that provide an implementation of the interface?
The brute force method would be to use "Find References" in Visual Studio and manually look through the results to separate out the usages from the implementations, but for an interface in a large codebase that is heavily referenced with relatively few implementations, this can be time consuming and error prone.
In Java, running javadoc on the codebase (using the -private option to include private classes) would generate a documentation page for the interface (e.g. Comparable) that includes all implementing classes for the interface as well as any subinterfaces (though it doesn't include implementing classes of the subinterfaces, these are relatively easy to determine by drilling down into the listed subinterfaces). It's this functionality that I'm looking for but with C# and Visual Studio.

In plain Visual Studio (since 2010) you can right click a method name (definition in interface or implementation in other class) and choose View Call Hierarchy. In Call Hierarchy window there is "Implements" folder where you can find all locations of the interface method implementation.

(Edit based on comment...)
If you have ReSharper installed:
In Visual Studio, right click on the type name and choose "Go to Inheritor". Alternatively, select the type name, then go to ReSharper/View/Type Hierarchy to open up a new tab. (The menu will show you the keyboard shortcut - this can vary, which is why I explained how to find it :)
If you don't have ReSharper:
You can use Reflector, which is able to show you all the type hierarchy very easily - just under the type name are expandable items for base types and derived types. Similar tools are available such as ILSpy and dotPeek.
Buy ReSharper - it's a great tool :)

Put the cursor to class or interface type and
CTRL + F12

With Visual Studio 2010+
Right click a member method and choose view call hierarchy. Expand the Implements folder. This lists all the types that implement the interface the method belongs to.
With Resharper 7
Right Click the interface > Navigate To > Derived Symbols. The symbols listed in bold derive directly from the interface. Non-bold symbols derive from a superclass.

For those using Visual Studio 2015, there is this awesome extension called Go To Implementation. Give it a try.
Once you've installed the extension, you can just right click at any occurrences of the interface (e.g. IUrlShortener) and click on Go To Implementation menu. If you only have one class that implements the interface, clicking the menu will bring you directly to the class. If you have more than one class that implements the interface, it will list all the classes.

For those using Visual Studio 2015, install Visual Studio 2015 Update 1 RC. From the Visual Studio blog:
Today we released Visual Studio 2015 Update 1 RC, which builds on the Update 1 CTP we released three weeks ago. In addition to the features introduced in the CTP as described here, the Release Candidate includes the following:
Go To Implementation. The feature many of you have been waiting for: just right-click on an interface or abstract method and select this command to navigate to the implementation.

I don't think this functionality is inbuilt into VS but IIRC Resharper has this.

You could do a regular expression search for the interface.
:(\ *[^},]+,)*\ *IMyInterfaceName
CTRL+SHIFT+F launches the following window:

I prefer the "Navigate To..." option. With your cursor on the function call, try the following:
Shortcut Key:
Ctrl+, (Ctrl+comma)
Menu:
Edit Menu
Click "Navigate To..."
Benefits:
Doesn't show all references like "Find All References"
Shows the
"type" of the implementation so it will note which is your interface

Use Shift + F12 to show all references, including the definitions.

If you use resharper ALT + END shortcut might help to find all Inheritors.

I've heard tell (no experience myself) that doxygen is to .Net as as javadoc is to java.

Related

How to find derived class in Visual Studio 2017?

I don't have Resharper installed.. I want to see all subclasses inherited from IActionResult and there is no things like show derived classes in object browser and class view.
I try to install a extension TypeHierarchyViewer(https://marketplace.visualstudio.com/items?itemName=munyabe.TypeHierarchyViewer) but it is not working (even using the example List, just remains blank) .
So what should I do?
I use this function frequently developing java application using eclipse..It seems there is no out-of-box tool in VS2017 or I just missed some things??
(Go To Implementation says the symbol has no implementations)
(Same in class view)
I have searched many "solutions"(like Visual Studio: How do I show all classes inherited from a base class?) but not work or need other tools(or just see the doc?).
I want to see if there anyway to do it just using VS.
Before I thought VS was a very good IDE but I can't image it lacks so much basic functions...(so there is Resharper...)
And I find there is derived types but in Solution explorer:
But you can't input the class you want And if I input IActionResult in search box it will not find it(not in the my source and I don't implement it).
After trying..I find VS support it in solution explorer...
But it's hard to use...
I need to find some classes or interface in my source code related to the class or interface I want and use derived type and implements to find the it..
It looks like:
(I find a class and navigate to object, it lists all classes .Then I find ActionResult and choose implements find IActionResult, finally I can see all derived classes above... ...)
emmmmm
it seems there is no direct way using VS(although I can get all sub classes using solution explorer but it's too verbose...) to get the result I want.
Finally I choose to use dotpeek(I don't want to buy resharper because I just study ASP.net core not for work)
I open C:\Program Files\dotnet\sdk\NuGetFallbackFolder in dotpeek where the dependencies located.
Then using Hierarchies get the result.
feel a little disappointed about VS. Not so strong before I heared.
Thx for help. :)

learning public methods of a class

I'm learning a bit of C# and I know a bit of c++ right now.
When I want to learn about a class's public methods - I look in the header file in C++- which C# doesn't have. Where do I get a good summary of a class's public methods without having to look in the source file?
In the documentation, in Visual Studio using the object browser or IntelliSense or using reflection (either at runtime or using third-party tools).
There are few ways:
1) Read the documentation - .NET Framework class library or the appropriate one for used library.
2) Take a look at class in Class view
3) Use some metadata reading tool like ILDasm
4) The last in the list, but the most useful and most used is Intellisense
C# doesn't have header files, but byte code-compiled assemblies (i.e. exe and dll files) contain a section with metadata about namespace and namespace members, which is used by the runtime to discover types, methods, properties and so on. Also, Visual Studio uses it to offer its powerful intellisense.
Anyway, if you press F12 when cursor is on a method name, or any member (class, enumeration, ...), or you right-click there, Visual Studio will go to the definition of the whole member, and if such member has no source code in your machine, it will show metadata only:

How do you auto-implement an interface in C#?

Previously in Visual Studio, if you declared a class with an interface you could put your cursor on the interface, right-click it and select Implement Interface. (See here for a similar question for VS 2008.)
I've started working on a project that has defined numerous interfaces that will be used with SimpleInjector. Now, as I am beginning to write my service classes, I've noticed that the Implement Interface menu option is gone.
How can I implement all necessary method stubs like I could in VS 2008?
Sorry if this is a duplicate, however, I couldn't find this answer anywhere.
EDIT NOTES - Aug 17, 2018
This has been a hot question over the years. I've updated the title and tags for this so that it covers all pertinent versions of C# and Visual Studio.
This was ultimately a coder FAIL on my part. Much like classes, interfaces when generated by VS aren't defined as public. You will have to manually modify the interface declaration with the public accessor since VS doesn't automatically add it. It's easy to forget this step because you don't have to set member modifiers within the interface since, by definition, they will all be public.
That hasn't changed. All you need to do is hover over the interface name in the class, and the blue underline bar should show up, giving you options to implement the interface. If the blue underline is not appearing, check to make sure the interface is accessible with what assemblies and namespaces you are referencing.
When you look at the interface name, you should see a little blue underline:
If you hover over it, a drop down menu will appear, letting you implement the interface:
Put your cursor somewhere in the interface text (the part after the colon) and hit Ctrl + .

Using Visual Studio DTE to analyze source code

There is an interesting coding standard in my shop that says that we don't shortcut type names with a using statement, they have to be fully qualified (so any time you reference a type, you use MyRootNamespace.ANamespace.MaybeAnotherNamespace.MyClassName, instead of just "MyClassName").
Love it or hate it, that's just how we roll, and I can't do anything about that.
Of course, you are swimming upstream with Visual Studio, because all the editing tools that generate code for you (member completion, event completion, etc) use the short type names wherever possible.
What I would like to do is to build some sort of extension or macro for Visual Studio that will correct a partial declaration, and replace it with the fully-qualified typename.
I started out trying to build a macro that would run for the symbol that your cursor is on (though I'd like to be able to scan a whole file, or maybe just intercept the code as you type).
I found that I can get members of a class with FileCodeModel2.GetElementFromPoint(), but this method will only work on class members -- it won't pick up a variable declaration inside of a method, for example.
Is there some other way I can get at that stuff? I'm currently using Macros, but would the new VS Extension model be more appropriate? I'm using Visual Studio 2010 Ultimate edition, and I only need to target c# code.
The object model allows you to do many things that are available within the IDE. But as such a feature is not available in the IDE you are out of luck here, I'm afraid.
The only thing you can do automatically with the using directives is sort them and remove the ones that are not used.
Update
As it seems it is somewhat possible to retrieve a fully qualified type name from a macro. However, it seems problematic with special cases such as generics.

How can I find which classes implement a given interface in Visual Studio?

I have a solution.
I have an interface.
I have several classes that implement the interface.
I can use "Find All References" in order to find where the interface is implemented, but it also returns results where the interface is the return type, or where a class explicitly implements an interface's method.
Is there a better way to quickly/easily find which classes implement the interface?
Using VS2010, with Productivity Power Tools (free) installed:
Leave debug mode if necessary
Hover over a reference to the interface
Expand the drop down that appears
"Implemented By"
Reflector (which used to be free) will show you this; load the dll and find the interface (F3) - expand the "Derived Types" node.
Why not just search in the entire solution for this:
\:.*MyInterface
and enable regular expressions in the search box - that ought to do the job.
Edit: Fixed the regex, it was for java before, whoops.
Using the "Go to Inheritor" feature in Resharper or a similar plugin would do the trick.

Categories

Resources