C#/.NET How do I find the containing namespace from a class name
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.
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.
CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.
You can always hang a big poster on your cube wall like me.
3.5 NameSpace
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.
This is where the Object Browser (Ctrl+W,J) comes in handy.
Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.
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.
Use the search function in .NET Reflector by Red Gate Software.
I generally use the offline MSDN reader, with the left panel set to the Index tab.
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.
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.
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.
Two ways that work in 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)
Select the namespace and it will insert it for you.
精彩评论