简体   繁体   中英

How to get the assembly name and class name with full namespace of a given class in the solution?

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!

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.

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.

You can use F# interative, just open the window (View -> Other Windows -> F# Interactive) and try the following:

> typedefof<System.Uri>.Assembly.FullName;;
    val it : string =
      "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

There are a few assemblies referenced automatically like System. If you need to get the name from another assembly, you'll need to add the reference first (like the service model for example).

> #r "System.ServiceModel.dll";;

--> Referenced 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.ServiceModel.dll'

> typedefof<System.ServiceModel.ICommunicationObject>.Assembly.FullName;;
val it : string =
  "System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

F# interative is part of Visual Studio 2010.

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

There you'll see all meta information of types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM