简体   繁体   English

如何在解决方案中获取具有给定类的完整命名空间的程序集名称和类名?

[英]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. 我正在使用WPF,并且经常需要获取给定类的名称空间和程序集名称。 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. 您可以使用Visual Studio 立即窗口作为获取其中一个解决方案项目的程序集限定名称的快速方法。

IIRC, these steps should work: IIRC,这些步骤应该有效:

  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 ; 打开立即窗口 ,默认C#环境快捷键是CTRL + D + I ;
  4. In the Immediate Window type typeof(ClassNameOfStep1).AssemblyQualifiedName and press Enter . 在立即窗口中键入typeof(ClassNameOfStep1).AssemblyQualifiedName并按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. 立即窗口依赖于设计时表达式评估,而后者依赖于Visual Studio托管过程,因此您需要启用它,默认情况下已启用。

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. 此外,我做了一些快速测试,除了我在VS 2008上尝试它时,类的名称在所有情况下都足够了,这要求我提供该类型的FullName So if Name results in error use the Name qualified with the namespace. 因此,如果Name导致错误,请使用Name命名空间。

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: 您可以使用F#interative,只需打开窗口(查看 - >其他Windows - > F#Interactive)并尝试以下操作:

> 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. 像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. F#interative是Visual Studio 2010的一部分。

You need Object Browser . 你需要Object Browser Press Ctrl + Alt + J . Ctrl + Alt + J.

There you'll see all meta information of types. 在那里你会看到所有类型的元信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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