在 Visual Studio 中,如何显示从基类继承的所有类?
例如,在 ASP.NET MVC 中有几个“ActionResult' 类型 - 它们都继承/实现基类 ActionResult
。
看起来除非您只是“知道”View
和 Json
是有效的 ActionResult
类型,否则您无法轻松找到此信息。
请证明我错了。
对象浏览器中是否有某些东西可以让这个问题很容易找到?
我什至寻求 Visual Studio 之外的工具建议,以发现有关各种类的信息。 例如:Resharper 中有什么可以帮助我的吗?
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?
发布评论
评论(19)
对于 VS2012,
For VS2012,
为此,您不一定需要 Reflector - Visual Studio 的“类图”视图还可以让您轻松找到特定类的所有派生类。 右键单击“类视图”中的类,然后选择“查看类图”。 如果图表没有显示您想要的层次结构的详细级别,请右键单击图表中类的框,然后选择“显示派生类”。
可能不像 ReSharper 那样直接,但如果您还没有 R#,它是一个选择。
不幸的是,我不确定哪些特定版本的 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.
当然,Resharper 可以做到这一点。 以及更多。
只需右键单击任意位置的类型名称,然后在上下文菜单中选择“转到继承者”即可。
“转到继承者”还可以应用于导航到覆盖的方法和接口方法的实现。 对于接口,您可以再次调用“查找高级用法”,只需右键单击)即可在其中查找所有扩展和实现。 对于类型 - 派生类型。
我最喜欢的功能 - 在任何类型/方法上按住 Control 单击以导航到其声明。
我认为它是.net开发人员必备的工具。
在 Resharper 9.2 中,在源代码中的任何类型上,按住 rt 键单击“查找高级用法”,选择 Find="Derived" 和 Scope="Solutions and Libraries"。
例如,要查找来自任何供应商的包含的 DLL 中某个基类的所有继承者(在库和代码中),请在代码中使用该基类声明一个变量。 然后右键单击您刚刚键入的基类名称。
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.
从“Visual Studio 2015 Update 1”开始,您只需右键单击类代码编辑器中的类名称,然后从上下文菜单中选择“转到实现”:快捷键为 Ctrl + F12。
请参阅 https有关更多详细信息,请访问://blogs.msdn.microsoft.com/dotnet/2015/11/30/whats-new-in-visual-studio-update-1-for-net-management-languages/。
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.
这是最不懒惰的答案(我只是为这个答案感到自豪:)
我没有ReSharper,之前尝试过但不想购买。
我尝试了类图,但根本不实用,因为层次图跨越了世界 3 倍,而且我的笔记本电脑屏幕没有无限宽度。
因此,我自然而简单的解决方案是编写一些 Windows 窗体代码来迭代程序集中的类型,并使用反射将节点添加到树视图,如下所示:
请假设您有一个文本框、一个树视图和运行此代码的表单所需的其他内容
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
还没人提到这一点,所以我会添加它。
Jetbrains dotPeek 是一款免费的 .NET 反编译器,能够轻松显示此信息。
免费下载:http://www.jetbrains.com/decompiler/
Jetbrains 是 Resharper 的开发公司。
查找派生类的步骤:
ActionResult 中键入
ActionResult
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:
ActionResult
ActionResult
class declaration, right-click and select 'Derived symbols'在 Visual Studio 类视图中,导航到您感兴趣的类并找到其基类和派生类。
In Visual Studio Class View, navigate to the class you are interested in and find its base and derived classes.
假设您安装了 Resharper:将光标放在类/接口上,右键单击 - 检查 - 层次结构
这将显示子类、实现和超类。
Assuming you have Resharper installed: with cursor on the class/interface, right click - Inspect - Hierarchies
This shows subclasses, implementations, and superclasses.
您还可以使用Reflector。
加载您希望其查找的所有程序集,导航到某个类型,然后展开“派生类型”项。
您也可以在对象浏览器中执行此操作,但由于某种原因 VS2008 只能在 .Net Framework 视图之一中执行此操作。 (VS2010 Beta 2在任何视图下都可以做到)
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)
对于框架类,我使用 MSDN 库。 继承层次结构部分是双向的。 诚然,对于某些 3d 方库来说并没有多大帮助。
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.
借助 ReSharper(版本 2016.1.2),只需 Ctrl+< kbd>Alt+单击基类上的。 您将看到类似这样的内容:
With the help of ReSharper (version 2016.1.2) just Ctrl+Alt+Click on the Base Class. You will see something like this:
将类或命名空间从对象资源管理器拖到类图文件中。
如何获取漂亮的类图内置 .net 类?
Drag class or namespace from Object Explorer to Class Digram file.
How to get a nice class diagram for built-in .net classes?
作为使用 Visual Assist 的选项,您还可以按 Shift + ALT + G(当插入符号位于类名称上时)调出“转到相关”上下文菜单,如果存在派生类,您将获得一个子菜单为此,它列出了所有类,甚至显示了这些派生类之间的继承结构。
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.
选择类型并右键单击查看某些内容 ---在代码图上显示 --选项 选择它为继承此类型的人生成图像格式作为非常好的图像作为 .dgml 文件格式它提供了有关类型的更多信息
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
2020 更新:
您可以使用 Visual Studio 的 代码映射效果很好。
只需右键单击该类并导航至:
代码图
>在代码图上显示相关项目
>显示所有派生类型
注意:
您可以通过下载企业预览版来免费获得它在这里。
PS 在您的计算机中保留企业版(或至少是预览版)确实值得,因为它有一堆 真正有用的工具,未包含在其他版本中。
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
Note:
you can get it for free by downloading the Enterprise Preview version from here.
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 code it.
如果您升级到VS2012,您可以使用“代码图”。
右键单击要查看其继承层次结构的类型,然后选择“添加到代码映射”。
然后在代码图视图中,您可以展开节点并右键单击以选择“显示派生/基类”。
不幸的是,它不适用于 .NET 提供的类。
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.
对于 Visual Studio 2012(不带 ReSharper),
For Visual Studio 2012 (Without ReSharper)
一个非常简单的方法是在“查找和替换窗口”(CTRL + F) 中搜索
: Classname
并使用“查找选项/匹配整个单词”选项。您将只找到继承的类。
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.