visual-studio C#/.NET 如何从类名中找到包含的命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1717814/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:12:34  来源:igfitidea点击:

C#/.NET How do I find the containing namespace from a class name

c#.netvisual-studio

提问by S.C. Madsen

I often find myself remembering the name of a class that I want to use, but not remembering the containing namespace. Apart from searching the web, i wonder if a good method exists for looking this up.

我经常发现自己记得要使用的类的名称,但不记得包含的命名空间。除了在网上搜索之外,我想知道是否存在查找此内容的好方法。

回答by

I think if you press ALT, SHIFT and F10 in Visual Studio - intellisense will drop down an option for you to add the name space of the class you have just typed.

我认为如果您在 Visual Studio 中按 ALT、SHIFT 和 F10 - 智能感知将下拉一个选项,供您添加刚刚键入的类的名称空间。

回答by Philip Wallace

CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.

CTRL + '.' 将打开一个菜单,您可以在其中添加“使用”或完全限定课程。

回答by J.Hendrix

You can always hang a big poster on your cube wall like me.

你总是可以像我一样在你的立方体墙上挂一张大海报。

3.5 NameSpace

3.5 命名空间

回答by Neil N

If you know the name of a class in .Net but have no idea what namespace it is in, it can be hard finding it, especially if you dont have a reference/using to the assembly containing it.

如果您知道 .Net 中的类的名称,但不知道它在哪个命名空间中,则可能很难找到它,尤其是如果您没有对包含它的程序集的引用/使用。

This is where the Object Browser(Ctrl+W,J) comes in handy.

这是对象浏览器(Ctrl+W,J) 派上用场的地方。

Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.

打开它,输入名称,它将为您提供所有匹配项,无论是在您的项目/解决方案中,还是在所有 .Net 框架中。

Edit:
As S.C. Madsen's comment points out, this also helps if you only remember PART of a class name, also if you only remember a method name but not the class.

编辑:
正如 SC Madsen 的评论指出的那样,如果您只记得类名的 PART,这也有帮助,如果您只记得方法名而不记得类。

回答by OrionRobillard

You can right click and select "Go To Definition" in VS and this will either load the class definiftion in your solution or it will show a metadata view of the class definition using reflection. Either of those should have the namespace defined near the top of the page.

您可以右键单击并在 VS 中选择“转到定义”,这将在您的解决方案中加载类定义,或者它将使用反射显示类定义的元数据视图。其中任何一个都应该在页面顶部附近定义命名空间。

If you need to add the namespace with a using decliration right click the unresolved class and mouse over to resolve. It will show you a list of namespaces that contain that class and selecting one will generate the using statement.

如果您需要使用 using 声明添加命名空间,请右键单击未解析的类并将鼠标悬停在其上进行解析。它将显示包含该类的命名空间列表,选择一个将生成 using 语句。

回答by S.C. Madsen

Use the search function in .NET Reflector by Red Gate Software.

使用 Red Gate Software 的 .NET Reflector 中的搜索功能。

回答by Jon Skeet

I generally use the offline MSDN reader, with the left panel set to the Index tab.

我通常使用离线 MSDN 阅读器,将左侧面板设置为“索引”选项卡。

Another option in Visual Studio is to type the name of the type as if you were declaring a variable, and then see what it suggests. If the name goes to a light blue colour (by default) then it's in one of the namespaces you're already importing - just hover over it to find out which. Otherwise, see what namespaces it offers to add using directives for.

Visual Studio 中的另一个选项是键入类型的名称,就像声明变量一样,然后查看它的建议。如果名称变为浅蓝色(默认情况下),则它位于您已经导入的命名空间之一中 - 只需将鼠标悬停在它上面即可找出是哪个。否则,看看它提供了哪些命名空间来添加 using 指令。

回答by Steven Hall

Two ways that work in Visual Studio 2013:

在 Visual Studio 2013 中的两种工作方式:

  • Right-click and select "Resolve".
  • Hover over the class and a 'Options to help bind the selected item' box will appear (same as Ctrl + '.' or Alt+Shift+F10)
  • 右键单击并选择“解决”。
  • 将鼠标悬停在类上,将出现“帮助绑定所选项目的选项”框(与 Ctrl + '.' 或 Alt+Shift+F10 相同)

Select the namespace and it will insert it for you.

选择命名空间,它将为您插入它。