Visual Studio: How do I show all classes inherited from a base class?
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?
For VS2012,
- Navigate to file in solution explorer
- Expand and select your class
- Right click the class item (not the file item) -> Derived Types
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.
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.
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.
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();