visual-studio Visual Studio:如何显示从基类继承的所有类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/282377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio: How do I show all classes inherited from a base class?
提问by Dan Esparza
In Visual Studio, How do I show all classes inherited from a base class?
在 Visual Studio 中,如何显示从基类继承的所有类?
For example, in ASP.NET MVC there are several 'ActionResult' types -- and they all inherit from / implement the base class ActionResult.
例如,在 ASP.NET MVC 中有几种“ ActionResult”类型——它们都继承自/实现基类ActionResult。
It looks like unless you just 'know' that Viewand Jsonare valid ActionResulttypes, there is no way you can easily find this information out.
看起来除非您只是“知道”View并且Json是有效ActionResult类型,否则您无法轻松找到此信息。
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 之外的工具提供建议,以发现有关各种类的信息。例如:Resharper 中有什么东西可以帮助我吗?
采纳答案by Shrike
Sure, Resharpercan do this. And much more.
当然,Resharper可以做到这一点。以及更多。
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.
只需在任何地方右键单击类型名称,然后在上下文菜单中选择“转到继承者”。“转到继承者”也可以应用于导航到覆盖和接口方法实现的方法。对于接口,您可以再次调用“Find Usages Advanced”,只需右键单击)可以找到所有扩展和实现的位置。对于类型 - 派生类型。还有我最喜欢的功能 - 在任何类型/方法上按住 Control 键单击以导航到其声明。
I think it's a must-have tool for .net developers.
我认为它是 .net 开发人员必备的工具。
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.
在 Resharper 9.2 中,在源代码中的任何类型上,rt-单击“Find Usage Advanced”,选择 Find="Derived" 和 Scope="Solutions and Libraries"。
例如,要在来自任何供应商的包含的 DLL 中查找某个基类的所有继承者(在库和您的代码中),请在您的代码中使用该基类声明一个变量。然后右键单击您刚刚键入的基类名称。
回答by countunique
For VS2012,
对于 VS2012,
- Navigate to file in solution explorer
- Expand and select your class
- Right click the class item (not the file item) -> Derived Types
- 导航到解决方案资源管理器中的文件
- 展开并选择您的班级
- 右键单击类项(不是文件项)-> 派生类型
回答by Michael Burr
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".
为此,您不一定需要 Reflector - Visual Studio 的“类图”视图也可以让您轻松找到特定类的所有派生类。在“类视图”中右键单击类并选择“查看类图”。如果图表未显示层次结构所需的详细级别,请右键单击图表中类的框,然后选择“显示派生类”。
Might not be as direct as Resharper, but it's an option if you don't have R# already.
可能不像 Resharper 那样直接,但如果您还没有 R#,它是一个选项。
Unfortunately, I'm not certain which particular versions of Visual Studio have it.
不幸的是,我不确定哪个特定版本的 Visual Studio 有它。
回答by Mzn
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:
我没有 ReSharper,以前试过但不想买。我尝试了类图,但根本不实用,因为层次结构图跨越世界 3 倍,而且我的笔记本电脑的屏幕没有无限宽度。所以我自然而简单的解决方案是编写一些 Windows 窗体代码来迭代程序集中的类型并使用反射将节点添加到树视图,如下所示:
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();
回答by Ahmad
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.
从“Visual Studio 2015 Update 1”开始,您只需右键单击类代码编辑器中的类名,然后从上下文菜单中选择“转到实现”:Ctrl + F12 是快捷方式。
See https://blogs.msdn.microsoft.com/dotnet/2015/11/30/whats-new-in-visual-studio-update-1-for-net-managed-languages/for more details.
有关更多详细信息,请参阅https://blogs.msdn.microsoft.com/dotnet/2015/11/30/whats-new-in-visual-studio-update-1-for-net-managed-languages/。
回答by Dan Esparza
Nobody has mentioned this yet, so I'll add it.
Jetbrains dotPeekis a free .NET decompiler has the power to show this information easily.
还没有人提到这个,所以我补充一下。
Jetbrains dotPeek是一款免费的 .NET 反编译器,能够轻松显示这些信息。
Free download: http://www.jetbrains.com/decompiler/
免费下载:http: //www.jetbrains.com/decompiler/
Jetbrains is the company that makes Resharper.
Jetbrains 是制造 Resharper 的公司。
Steps to find derived classes:
查找派生类的步骤:
- Start up dotPeek
- Select 'Open from GAC...' and add the System.Web.MVC assembly
- Select 'Navigate / Go to type' and type in
ActionResult - On the
ActionResultclass declaration, right-click and select 'Derived symbols' - Voila! Every single derived class is shown (even a few I didn't know about!)
- 启动 dotPeek
- 选择“从 GAC 打开...”并添加 System.Web.MVC 程序集
- 选择“导航/转到类型”并输入
ActionResult - 在
ActionResult类声明上,右键单击并选择“派生符号” - 瞧!显示了每个派生类(甚至是一些我不知道的类!)
回答by Tim
回答by sigirisetti
回答by SLaks
You can also use Reflector.
您也可以使用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)
您也可以在对象浏览器中执行此操作,但出于某种原因,VS2008 只能在 .Net Framework 视图之一中执行此操作。(VS2010 Beta 2 任何视图都可以)
回答by Devid
With the help of ReSharper(version 2016.1.2) just Ctrl+Alt+Clickon the Base Class. You will see something like this:
的帮助下ReSharper的(版本2016年1月2日)刚刚Ctrl+ Alt+Click基类。你会看到这样的事情:


