Visual Studio: How do I show all classes inherited from a base class?

Visual StudioClassAbstract Class

Visual Studio Problem Overview


In Visual Studio, How do I show all classes inherited from a base class?

For example, in ASP.NET MVC there are several 'ActionResult' types -- and they all inherit from / implement the base class ActionResult.

It looks like unless you just 'know' that View and Json are valid ActionResult types, there is no way you can easily find this information out.

Please prove me wrong.

Is there something in the object browser that makes this easy to find out?

I'm even up for suggestions of tools outside of Visual Studio to discover this information about various classes. For example: is there something in Resharper that will help me out?

Visual Studio Solutions


Solution 1 - Visual Studio

For VS2012,

  1. Navigate to file in solution explorer
  2. Expand and select your class
  3. Right click the class item (not the file item) -> Derived Types

Solution 2 - Visual Studio

You don't necessarily need Reflector for this - Visual Studio's "Class Diagram" view will let you easily find all derived classes for a particular class as well. Right click on the class in "Class View" and choose "View Class Diagram". If the diagram doesn't show the level of detail you want for the hierarchy, right click on the box for the class in the diagram, and choose "Show Derived Classes".

Might not be as direct as ReSharper, but it's an option if you don't have R# already.

Unfortunately, I'm not certain which particular versions of Visual Studio have it.

Solution 3 - Visual Studio

Sure, Resharper can do this. And much more.

Just right click on type name in any place and choose "Go To Inheritor" in context menu. "Go To Inheritor" can be also applied to method for navigating to overrides and an interface method's implementations. For an interface you could call "Find Usages Advanced" again, just right click) where to find all extendings and implementations. For a type - derived types. And my favorite feature - click with holding Control on any type/method for navigating to its declaration.

I think it's a must-have tool for .net developers.


In Resharper 9.2, on any type in source code, rt-click "Find Usage Advanced", select Find="Derived" and Scope="Solutions and Libraries".
For example, to find all inheritors (both in the library and your code) of some base class in an included DLL from any vendor, declare a variable in your code with that base class. Then right-click on that base class name you just typed.

Solution 4 - Visual Studio

Starting from 'Visual Studio 2015 Update 1' you can simply right click on a class name in the class code editor and then select 'Go To Implementation" from the context menu : with Ctrl + F12 being the shortcut.

See https://blogs.msdn.microsoft.com/dotnet/2015/11/30/whats-new-in-visual-studio-update-1-for-net-managed-languages/ for more details.

Solution 5 - Visual Studio

This is the least lazy answer (I'm just proud of this answer :)

I don't have ReSharper, tried it before but didn't want to buy it. I tried a class diagram but is not practical at all because the hierarchy diagram spans the world 3 times over and my laptop's screen does not have infinite width. So my natural and easy solution was to write some Windows Forms code to iterate over the types in an assembly and use reflection to add nodes to a tree view, as follows:

please assume you have a text box, a tree view and other things needed on a form in which this code runs

//Go through all the types and either add them to a tree node, or add a tree
//node or more to them depending whether the type is a base or derived class.
//If neither base or derived, just add them to the dictionary so that they be
//checked in the next iterations for being a parent a child or just remain a
//root level node.

var types = typeof(TYPEOFASSEMBLY).Assembly.GetExportedTypes().ToList();
Dictionary<Type, TreeNode> typeTreeDictionary = new Dictionary<Type, TreeNode>();
foreach (var t in types)
{
    var tTreeNode = FromType(t);
    typeTreeDictionary.Add(t, tTreeNode);

    //either a parent or a child, never in between
    bool foundPlaceAsParent = false;
    bool foundPlaceAsChild = false;
    foreach (var d in typeTreeDictionary.Keys)
    {
        if (d.BaseType.Equals(t))
        {
            //t is parent to d
            foundPlaceAsParent = true;
            tTreeNode.Nodes.Add(typeTreeDictionary[d]);
            //typeTreeDictionary.Remove(d);
        }
        else if (t.BaseType.Equals(d))
        {
            //t is child to d
            foundPlaceAsChild = true;
            typeTreeDictionary[d].Nodes.Add(tTreeNode);
        }
    }

    if (!foundPlaceAsParent && !foundPlaceAsChild)
    {
        //classHierarchyTreeView.Nodes.Add(tn);
    }
}

foreach (var t in typeTreeDictionary.Keys)
{
    if (typeTreeDictionary[t].Level == 0)
    {
        classHierarchyTreeView.Nodes.Add(typeTreeDictionary[t]);
    }
}

StringBuilder sb = new StringBuilder();
foreach (TreeNode t in classHierarchyTreeView.Nodes)
{
    sb.Append(GetStringRepresentation(t, 0));
}
textBox2.Text = sb.ToString();

Solution 6 - Visual Studio

Nobody has mentioned this yet, so I'll add it.
Jetbrains dotPeek is a free .NET decompiler has the power to show this information easily.

Free download: http://www.jetbrains.com/decompiler/

Jetbrains is the company that makes Resharper.

Steps to find derived classes:

  1. Start up dotPeek
  2. Select 'Open from GAC...' and add the System.Web.MVC assembly
  3. Select 'Navigate / Go to type' and type in ActionResult
  4. On the ActionResult class declaration, right-click and select 'Derived symbols'
  5. Voila! Every single derived class is shown (even a few I didn't know about!)

Solution 7 - Visual Studio

Try Visual Studio Class View

In Visual Studio Class View, navigate to the class you are interested in and find its base and derived classes.

Solution 8 - Visual Studio

Assuming you have Resharper installed: with cursor on the class/interface, right click - Inspect - Hierarchies

This shows subclasses, implementations, and superclasses.

Solution 9 - Visual Studio

You can also use Reflector.

Load all of the assemblies you want it to look in, navigate to a type, and expand the Derived Types item.

You can also do this in the Object Browser, but for some reason VS2008 can only do it in one of the .Net Framework views. (VS2010 Beta 2 can do it in any view)

Solution 10 - Visual Studio

For Framework classes, I use the MSDN Library. The Inheritance Hierarchy section goes in both directions. Admittedly not much help for some 3d party libraries, though.

Solution 11 - Visual Studio

With the help of ReSharper (version 2016.1.2) just Ctrl+Alt+Click on the Base Class. You will see something like this:

see derived classes reSharper

Solution 12 - Visual Studio

Drag class or namespace from Object Explorer to Class Digram file.

How to get a nice class diagram for built-in .net classes?

Solution 13 - Visual Studio

As an option for those using Visual Assist, you can also press Shift + ALT + G (while the caret is on the class name) to bring up the "Goto Related" context menu, and if there are derived classes you will get a submenu for that that lists them all out and even shows inheritance structures among those derived classes.

Solution 14 - Visual Studio

select the type and right click see something ---show on code map --option select it generate the image formet for those who inherit this type as very nice image as .dgml file format it gives much more info about type

Solution 15 - Visual Studio

Just code it.

Assembly asm = Assembly.LoadFrom(PATH_TO_PROJECT_OUTPUT);

var types = from t in asm.GetTypes()
            where t.BaseType != null && t.BaseType.ToString() == "System.Web.UI.Page"
            // if you want to add reference - where typeof (System.Web.UI.Page).Equals(t.BaseType) 
            select t;

foreach (var type in types)
{
    Console.WriteLine(type);
}

Solution 16 - Visual Studio

if you upgraded to VS2012 you can use the "Code Map".
Right click on the type you wish to view the inheritance hierarchy of and choose "Add to code map". Then in the code map view you can expand nodes and right click to choose "Show Derived/Base classes". Unfortunately it does not work for the .NET provided classes.

Solution 17 - Visual Studio

For Visual Studio 2012 (Without ReSharper)

  1. In Solution Explorer right click on the class (whose derived classes you want to see).
  2. In the class diagram, right click on the class and select 'Show derived classes'.

Solution 18 - Visual Studio

2020 update:

You can use Visual Studio's Code Maps which works pretty fine.

Simply right click on the class and navigate to:
Code Map > Show Related Items on Code Map > Show All Derived Types

enter image description here

Note:

  • This feature is available only on the Enterprise edition of the Visual Studio, and if you don't have, you can get it for free by downloading the Enterprise Preview version from here.
  • Or you might just ask anyone from your team share the code map with this feature if they have that edition.

P.S. It really worths to keep the Enterprise version (or at least the Preview version of it) in your machine, as it has bunch of really useful tools that are not included in the other editions.

Solution 19 - Visual Studio

A very simple way to do this, is to search for : Classname in the "Find and Replace Window" (CTRL + F) and use the "Find Options/Match whole word" Option. You will find only the inherited classes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan EsparzaView Question on Stackoverflow
Solution 1 - Visual StudiocountuniqueView Answer on Stackoverflow
Solution 2 - Visual StudioMichael BurrView Answer on Stackoverflow
Solution 3 - Visual StudioShrikeView Answer on Stackoverflow
Solution 4 - Visual StudioAhmadView Answer on Stackoverflow
Solution 5 - Visual StudioMznView Answer on Stackoverflow
Solution 6 - Visual StudioDan EsparzaView Answer on Stackoverflow
Solution 7 - Visual StudiosigirisettiView Answer on Stackoverflow
Solution 8 - Visual StudioTimView Answer on Stackoverflow
Solution 9 - Visual StudioSLaksView Answer on Stackoverflow
Solution 10 - Visual StudioTom JuergensView Answer on Stackoverflow
Solution 11 - Visual StudioDevidView Answer on Stackoverflow
Solution 12 - Visual StudioImanView Answer on Stackoverflow
Solution 13 - Visual StudiovdwtannerView Answer on Stackoverflow
Solution 14 - Visual StudioMagesh KView Answer on Stackoverflow
Solution 15 - Visual StudioMehmet AtaşView Answer on Stackoverflow
Solution 16 - Visual StudioAndreiView Answer on Stackoverflow
Solution 17 - Visual StudioAbhijeet NagreView Answer on Stackoverflow
Solution 18 - Visual StudioJust ShadowView Answer on Stackoverflow
Solution 19 - Visual StudioRichardView Answer on Stackoverflow