简体   繁体   English

给定Visual Studio中的一个类,如何找出包含它的dll?

[英]Given a class in Visual Studio, how to find out which dll contains it?

Working on modifying an existing .NET project. 致力于修改现有的.NET项目。 Given a C# class in Visual Studio, how can I find out which DLL has it? 给定Visual Studio中的C#类,如何找出它有哪些DLL?

The problem is that I want to use a method in this class in a new project so I need to find the correct DLL to add as a reference for the new project. 问题是我想在新项目中使用此类中的方法,因此我需要找到正确的DLL作为新项目的参考。

Search for your class name in the object browser (Menu View -> Object Browser). 在对象浏览器中搜索您的类名(菜单视图 - >对象浏览器)。 The assembly information window will contain the dll complete path. 程序集信息窗口将包含dll完整路径。

You can see the assembly in the project properties. 您可以在项目属性中查看程序集。 The output name of the DLL (which is what a Class Library project produces) is specified in there. DLL的输出名称(类库项目生成的名称)在那里指定。

There are some tools like ReSharper that will suggest adding using statements and even add a reference to the project. 有些工具如ReSharper会建议添加using语句甚至添加对项目的引用。 It seems you don't have something like that. 看来你没有那样的东西。

Adding a reference for a Microsoft class 添加Microsoft类的引用

In that case (before I had tools), I google for 在那种情况下(在我有工具之前),我谷歌搜索

class <ClassName> site:msdn.microsoft.com

eg 例如

class Process site:msdn.microsoft.com

which brings me to the Process class . 这让我进入了Process类 On the MSDN page, it lists the namespace and assembly: 在MSDN页面上,它列出了命名空间和程序集:

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

Adding a reference of your own class in a different project 在不同的项目中添加您自己的类的引用

  • Open the class definition (source code) 打开类定义(源代码)
  • Open the Solution Explorer 打开Solution Explorer
  • Click the "Sync with active document" button 单击“与活动文档同步”按钮
  • 与活动文档同步
  • Move up to the project 上移到项目
  • Open properties ( Alt + Enter ) 打开属性( Alt + Enter
  • Look at Application/Assembly Name 查看应用程序/程序集名称

This can be rather frustrating and irritating - I found myself in a situation where I needed an answer to the OP's question and none of the above answers worked for me. 这可能是相当令人沮丧和恼人的 - 我发现自己处于这样一种情况,我需要回答OP的问题,而且上述答案都不适合我。

I had a program that imported some Roslyn DLLs. 我有一个导入一些Roslyn DLL的程序。 Later, after I'd become unsure as to which DLLs were really necessary, I was trying to find out which classes were in which DLLs. 后来,在我不确定哪些DLL真正需要之后,我试图找出哪些类在哪些DLL中。

For example, my program referenced class MSBuildWorkspace . 例如,我的程序引用了类MSBuildWorkspace Navigating to the class declaration I can see the namespace name, and the fully qualified name of the class is Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace . 导航到类声明我可以看到命名空间名称,该类的完全限定名称是Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace

Opening the Object Browser, as suggested by @Spell, and searching for Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace results in "Search found no results", which I'm guessing must be due to a bug in Visual Studio. 按照@Spell的建议打开对象浏览器,并搜索Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace导致“搜索未找到结果”,我猜测必须是由于Visual Studio中的错误。

Fortunately, searching for just MSBuildWorkspace does find the class, but it still didn't tell me which DLL contains the class. 幸运的是,只搜索MSBuildWorkspace会找到该类,但它仍然没有告诉我哪个DLL包含该类。

So what I needed to do was to click on the class name, which is shown as a link, in the smallish panel in the lower right part of Object Browser. 所以我需要做的是在对象浏览器右下角的小面板中单击类名称(显示为链接)。 This results in the left panel switching to a view of the DLLs and the classes they contain. 这导致左面板切换到DLL及其包含的类的视图。 Now I could see that class MSBuildWorkspace was in DLL Microsoft.CodeAnalysis.Workspaces , and right-clicking on that and selecting Copy and pasting the result into a text editor provided the full path to the DLL. 现在我可以看到类MSBuildWorkspace在DLL Microsoft.CodeAnalysis.Workspaces ,右键单击它并选择Copy并将结果粘贴到文本编辑器中,提供了DLL的完整路径。

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

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