Visual Studio:如何显示从基类继承的所有类?

发布于 2024-07-08 13:42:00 字数 502 浏览 6 评论 0 原文

在 Visual Studio 中,如何显示从基类继承的所有类?

例如,在 ASP.NET MVC 中有几个“ActionResult' 类型 - 它们都继承/实现基类 ActionResult

看起来除非您只是“知道”ViewJson 是有效的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

长伴 2024-07-15 13:42:00

对于 VS2012,

  1. 导航到解决方案资源管理器中的文件
  2. 展开并选择您的类
  3. 右键单击类项(不是文件项)-> 派生类型

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
柠檬色的秋千 2024-07-15 13:42:00

为此,您不一定需要 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.

暗藏城府 2024-07-15 13:42:00

当然,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.

身边 2024-07-15 13:42:00

从“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.

空心空情空意 2024-07-15 13:42:00

这是最不懒惰的答案(我只是为这个答案感到自豪:)

我没有ReSharper,之前尝试过但不想购买。
我尝试了类图,但根本不实用,因为层次图跨越了世界 3 倍,而且我的笔记本电脑屏幕没有无限宽度。
因此,我自然而简单的解决方案是编写一些 Windows 窗体代码来迭代程序集中的类型,并使用反射将节点添加到树视图,如下所示:

请假设您有一个文本框、一个树视图和运行此代码的表单所需的其他内容

//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();

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();
只是我以为 2024-07-15 13:42:00

还没人提到这一点,所以我会添加它。
Jetbrains dotPeek 是一款免费的 .NET 反编译器,能够轻松显示此信息。

免费下载:http://www.jetbrains.com/decompiler/

Jetbrains 是 Resharper 的开发公司。

查找派生类的步骤:

  1. 启动 dotPeek
  2. 选择“从 GAC 打开...”并添加 System.Web.MVC 程序集
  3. 选择“导航/转到类型”并在 ActionResult 中键入 ActionResult
  4. >ActionResult 类声明,右键单击并选择“派生符号”
  5. 瞧! 显示了每个派生类(甚至有一些我不知道的!)

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!)
断桥再见 2024-07-15 13:42:00

尝试视觉效果Studio 类视图

在 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.

毁梦 2024-07-15 13:42:00

假设您安装了 Resharper:将光标放在类/接口上,右键单击 - 检查 - 层次结构

这将显示子类、实现和超类。

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

This shows subclasses, implementations, and superclasses.

烟柳画桥 2024-07-15 13:42:00

您还可以使用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)

舂唻埖巳落 2024-07-15 13:42:00

对于框架类,我使用 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.

平生欢 2024-07-15 13:42:00

借助 ReSharper(版本 2016.1.2),只需 Ctrl+< kbd>Alt+单击基类上的。 您将看到类似这样的内容:

查看派生类 reSharper

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

梦罢 2024-07-15 13:42:00

将类或命名空间从对象资源管理器拖到类图文件中。

如何获取漂亮的类图内置 .net 类?

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

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

眼趣 2024-07-15 13:42:00

作为使用 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.

掩于岁月 2024-07-15 13:42:00

选择类型并右键单击查看某些内容 ---在代码图上显示 --选项 选择它为继承此类型的人生成图像格式作为非常好的图像作为 .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

很酷不放纵 2024-07-15 13:42:00

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

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.

一瞬间的火花 2024-07-15 13:42:00

只需编码即可。

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);
}

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);
}
昔梦 2024-07-15 13:42:00

如果您升级到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.

鼻尖触碰 2024-07-15 13:42:00

对于 Visual Studio 2012(不带 ReSharper),

  1. 在解决方案资源管理器中右键单击该类(您想要查看其派生类)。
  2. 在类图中,右键单击该类并选择“显示派生类”。

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'.
分分钟 2024-07-15 13:42:00

一个非常简单的方法是在“查找和替换窗口”(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文