165

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?

Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
  • 2
    With Visual C# 2010 Express I search the solution for ": ActionResult". Not ideal, but better than nothing. – yoyo Apr 26 '12 at 21:52
  • 2
    I think you misunderstand my question. JsonResult (reference here: http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult.aspx) inherits from ActionResult. There are several other classes that inherit from ActionResult. What is an easy way to see all the classes that inherit from ActionResult? – Dan Esparza Apr 26 '12 at 22:09
  • 4
    In this case, the code that I'm using isn't code that appears directly in my solution. They are MVC framework classes in another assembly. Searching for ": ActionResult" won't work for any code I use from an assembly. – Dan Esparza Apr 26 '12 at 22:12

19 Answers19

184

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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
countunique
  • 4,068
  • 6
  • 26
  • 35
  • 14
    This assumes source code you wrote. How would this work with the example stated in the original question? (ActionResult types) – Dan Esparza Jul 01 '13 at 14:14
  • 1
    If you have any type that derives from ActionResult then you can right-click on your type and select Base Types, which will take you to ActionResult, which you can now right-click on and select Derived Types. (If you have no type that derives from ActionResult then you could work your way back to System.Object and then down to ActionResult.) – yoyo May 06 '14 at 19:03
  • 33
    For those of you who, like me, right clicked on the class's code file and were stumped at why there was no "derived types" menu option: recall that you have to first expand the code file item to reveal the class item--then right click. – BCA Dec 19 '14 at 13:15
  • 3
    I am using VS 2017. Unfortunately, this only shows the classes that exist in the same assembly, it doesn't show the entire solution. – NightOwl888 Oct 04 '17 at 19:18
  • 1
    The feature is not there anymore in **VS2019**. I'm just having VS2017 and VS2019 opened side by side and can find the "Derived Types" in the '17 version but not in the '19. :/ Can you kindly update the answer for the 2019 version? The Derived Types were moved in the **Class View**. Expand your class to see two folders: "Base Types" and "Derived Types". – Viktor Be May 05 '21 at 12:29
  • 2
    @ViktorBe Still working in VS2019 for me. – Bill Tür stands with Ukraine Sep 21 '21 at 11:47
  • 1
    The feature is still available in VS2019. – Scott Siddall Jan 25 '22 at 16:22
  • Still working in VS2022, thanks. – RonRonDK May 31 '22 at 09:31
78

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 20
    This will only let you view the classes within your solution that inherit/implement the target class/interface. Good to know though. – Markus Jarderot May 11 '11 at 17:55
  • Do you know about limitations of this method? Because it doesn't allows me to see all the subclasses (it seems it only shows subclasses that doesn't implement any additional interfaces). – Serge May 27 '13 at 08:07
  • 1
    I got an error saying "There are no types that derive from XXX in the current project", so I think it's limited to types in the current project, not the solution – Andy Mar 09 '16 at 12:54
  • Can I perform "Show derived classes" recursively? Or do I have to select "Show derived classes" on every sub class explicitly? – James Wierzba Jul 30 '18 at 21:45
48

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.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Shrike
  • 9,218
  • 7
  • 68
  • 105
  • 7
    Such a shame. Java developers that use the free Eclipse, have this functionality out of the box. In my Resharper + VS2015, I see "go to Implementation" rather than "Go To Inheritor". – Northern Pole Feb 22 '16 at 16:23
  • 1
    But this doesn't give me the classes in the middle, only leaves. CTRL+ALT+B would give the answer. – Northern Pole Feb 22 '16 at 17:09
  • 3
    Visual Studio 6 had it on C++ projects nearly 20 years ago. The object browser and class diagram don't provide the same light weight ability to flip inheritance trees. I recently found that Eclipse has a great version of this, with Java at least. Come on MS, phone up one of your ex developers and ask what it was that you had but lost....Granted it required all the projects to have the generate BSC browse data after compile option, but that was fine. It really was. – Philip Beck Mar 12 '16 at 09:20
19

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.

Ahmad
  • 1,462
  • 15
  • 23
  • 1
    I appreciate the tip, but I'm not sure this addresses my original use case of seeing all inherited classes from a base class. – Dan Esparza Jun 16 '17 at 13:38
  • 3
    @DanEsparza: it does, at least in VS2017. So I upvoted the answer – João Antunes Sep 06 '17 at 11:28
  • 1
    It's nice. Unfortunately, doesn't show the hierarchy tree... I just lists all in the same tree level. – Lombas Feb 28 '19 at 18:02
  • 1
    And as always the name is bogus, completely overlooked it during my search/use, had to come to StackOverflow to find it! – Dimitar Feb 18 '20 at 10:40
  • Perfect! Lists all subclasses in the "References" panel, which is familiar to me. But I also completely overlooked it, and I still don't get why "go to implementation" should mean "list all subclasses", but here we are. – Armin Rigo Sep 30 '21 at 20:25
14

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();
Lzh
  • 3,585
  • 1
  • 22
  • 36
  • 2
    +1 for a cool code example. What about base classes & derived classes in other assemblies? – ToolmakerSteve May 03 '14 at 05:12
  • @ToolmakerSteve, how about `AppDomain.CurrentDomain.GetAssemblies()`? Take a look at this: https://stackoverflow.com/questions/851248/c-sharp-reflection-get-all-active-assemblies-in-a-solution – Lzh Feb 28 '19 at 19:55
10

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!)
Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
9

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
sigirisetti
  • 329
  • 2
  • 10
  • 3
    This is actually a potentially useful view. Sadly, it's incredibly slow, does not link/follow the current editor, and generally feels like an afterthought in VS. – E-Riz Oct 25 '17 at 12:31
  • This shows all classes, not just the ones in your solution and works right out of the box. It's not slow. For a more graph-like view use Class Diagram built-in feature as well (but that only displays classes within your solution). This is all I would ever need and this is the best answer. – KeyC0de Jun 07 '20 at 23:38
  • 1
    Why do I only have **Base Types**, not **Derived Types** even when I'm sure they exist? Solution Explorer does have **Derived Types**...?! I guess I would need the Ultimate edition. – Michel de Ruiter Mar 03 '21 at 09:30
  • VS 2022 (Community) also seems to lack the "Derived Types" ! – MemeDeveloper Apr 19 '23 at 14:27
7

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

This shows subclasses, implementations, and superclasses.

onedaywhen
  • 55,269
  • 12
  • 100
  • 138
Tim
  • 84
  • 1
  • 1
  • Hmmm ... interesting, but not what I was looking for. If I right click on a returned ActionResult item and follow these steps, I get the 'Inspect' options for 'Controller' (the type of the containing class). If I try to go to the 'ViewResult' definition, I'm in metadata and I don't have the option to Inspect Hierarchies. – Dan Esparza Oct 19 '12 at 19:02
  • 1
    Also: I'm fairly certain this is a function provided by Resharper, it's not natively in Visual Studio... – Dan Esparza Oct 19 '12 at 19:08
5

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)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 3
    Reflector now costs money, but ILSpy will do the same for free. – yoyo Apr 26 '12 at 21:53
  • There is a 30-day free trial, but no free version you can use indefinitely. Unless you know something I don't? I did just check http://www.reflector.net. – yoyo May 08 '12 at 16:39
  • 1
    Sorry; Reflector used to still have a free edition. When did they get rid of it? – SLaks May 08 '12 at 17:37
  • February 2, 2011 -- http://www.zdnet.com/blog/burnette/red-gate-we-could-not-make-the-free-model-work-for-us-as-a-commercial-company/2176 – yoyo May 24 '12 at 19:22
  • 3
    What was truly horrifying, is that after that date, if you ran a copy of the free version you had previously downloaded, redgate (they bought Reflector) DELETED the old free version, replacing it with the trial. Without any warning. And no option to avoid the "upgrade". That's right: I hadn't gone to the company's website and downloaded anything from them. But there was licensing logic in the old ReSharper that was redirected to them, and allowed them to deactivate the copy I had. I vowed to never, ever, under any circumstances buy anything from redgate, or otherwise encourage their existence. – ToolmakerSteve May 03 '14 at 05:18
  • @ToolmakerSteve: Actually, that logic was in Reflector before RedGate bought it. – SLaks May 04 '14 at 01:34
  • @ToolmakerSteve I am under the same... uh... boycott? I refuse to purchase or use RedGate tools. Off topic, I know but: This tool is basically Reflector++ for free: http://www.jetbrains.com/decompiler/ – Brandon Sep 11 '14 at 15:02
4

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.

Tom Juergens
  • 4,492
  • 3
  • 35
  • 32
  • Here is an example for System.Exception: http://msdn.microsoft.com/en-us/library/system.systemexception(v=vs.110).aspx#inheritanceContinued – bnieland Jan 23 '14 at 20:13
4

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

Devid
  • 1,823
  • 4
  • 29
  • 48
  • 1
    +1 Nice! So it seems the class names that appear in bold in the list are **directly** inheriting from the selected base class, and the class names in the list that aren't bold are **indirectly** inheriting from the selected base class? – Simon Bosley Aug 26 '16 at 09:37
  • Exactly. Bald will bei the directly inheriting classes. – Devid Aug 26 '16 at 13:54
3

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

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

Community
  • 1
  • 1
Iman
  • 17,932
  • 6
  • 80
  • 90
2

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.

vdwtanner
  • 197
  • 2
  • 14
1

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

Magesh K
  • 11
  • 2
0

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);
}
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
  • 2
    Unfortunately this only tells you the types that directly inherit from a particular class, not the ones that are indirectly inherit from a class. For that you'd probably have to use something like what MZN wrote. – user420667 Aug 09 '12 at 15:15
  • Or you could use t.IsSubClassOf(targetclass). – user420667 Aug 15 '12 at 04:54
0

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.

Andrei
  • 2,282
  • 26
  • 35
  • 4
    FYI to anyone else, this only applies to VS2012 Ultimate with Update 1 installed. Premium/Pro folks can't create these maps, unfortunately. – Sean Hanley Apr 05 '13 at 16:11
0

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'.
Abhijeet Nagre
  • 876
  • 1
  • 11
  • 21
  • 1
    Again, as far as I can tell this will only show derived classes that you wrote. How would this work with the example stated in the original question? (ActionResult types) – Dan Esparza Sep 18 '13 at 19:51
0

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.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • 1
    It seems this only works for types you have the source for. Not things like `ActionResult` (the OP's example) – Iain Apr 16 '20 at 14:01
-1

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.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52