views:

71

answers:

3

I'm working with WPF and often have the need to get the namespace and assembly name of a given class. But I don't know how to do this.

So, how can we get the names when browsing the classes in the Solution Explorer or/and when editing the class's codes? Please help!

+1  A: 

These should give you what you're after:

var assemblyName = typeof(ClassNameGoesHere).AssemblyQualifiedName;
var namespaceOfClass = typeof(ClassNameGoesHere).Namespace;

I see you've just added a note to your question regarding "when browsing the classes in the Solution Explorer", the simple answer is that as far as I know, you can't because that's not what Solution Explorer is for (it's there for browsing the files in a solution, not what's contained inside them) and also because:

  1. One file can contain multiple classes
  2. All files in one project will, generally, always compile down to a single assembly, making it redundant to display that name for each file.

You may want to see if the "Class View" gives you what you want, but, I suspect it won't.

Rob
Hi Rob,I'm looking for a no-coding solution. Thank you anyway.
Nam Gi VU
@Nam Gi VU, I've added an update to take into account your addition to the question regarding Solution Explorer.
Rob
@Rob: I see. I've tried in the Class View but no help there. I've updated to add the case: when editing the class's codes.
Nam Gi VU
A: 

You need Object Browser. Press Ctrl + Alt + J.

There you'll see all meta information of types.

this. __curious_geek
Can you explain more about which meta information I should look for?
Nam Gi VU
+2  A: 

You could use the Visual Studio Immediate Window as a quick way to obtain the assembly qualified name for one of the solution projects.

IIRC, these steps should work:

  1. Open the file of a class contained in the project for which you want to obtain assembly name;
  2. Set that project as the startup project for the solution;
  3. Open the Immediate Window, default C# environment shortcut is CTRL+D+I;
  4. In the Immediate Window type typeof(ClassNameOfStep1).AssemblyQualifiedName and press Enter.

The Immediate Window depends on Design-Time Expression Evaluation which in turns depends of Visual Studio Hosting Process so you need to have it enabled, which by default already is.

Also, I did some quick tests and the Name of the class was sufficient in all cases except when I tried it on VS 2008, which required me to provide the FullName of the type. So if Name results in error use the Name qualified with the namespace.

João Angelo
I tried, but got error "The type or namespace name 'MyClass' is not valid in this scope"
Nam Gi VU
@Nam Gi VU, instead of `MyClass` try to use `MyNamespace.MyClass`.
João Angelo
@Angelo: I put in the full namespace of the class but doesn't work either. I got "'CCC' does not exist in the namespace 'A.BB'" when I pass in typeof(A.BB.CCC.DDDD).AssemblyQualifiedName :)
Nam Gi VU
@Nam Gi VU Make sure your application is running
tjrobinson
@Nam Gi Vu, I updated my answer with a missing step (2). Check if that solves the error. It should resolve it, but even if it does this new step kind of removes the simplicity of the solution.
João Angelo
@Angelo: I got it now. Thanks to a colleague of mine idea: need to run the solution and while the application is running, proceed your suggestion steps above. Thank you Angelo.
Nam Gi VU
Don't need to run guys. Just do step 2 (set the start-up project).
Nam Gi VU