如何:使用引用管理器添加或删除引用How to: Add or remove references by using the Reference Manager

可以使用“引用管理器” 对话框添加和管理对你、Microsoft 或其他公司开发的组件的引用。You can use the Reference Manager dialog box to add and manage references to components that you, Microsoft, or another company developed. 如果要开发通用 Windows 应用,你的项目将自动引用所有正确的 Windows SDK DLL。If you're developing a Universal Windows app, your project automatically references all of the correct Windows SDK DLLs. 如果要开发 .NET 应用程序,你的项目将自动引用 mscorlib.dll 。If you are developing a .NET application, your project automatically references mscorlib.dll. 一些 .NET API 在必须手动添加的组件中进行公开。Some .NET APIs are exposed in components that you have to add manually. 对 COM 组件或自定义组件的引用必须手动添加。References to COM components or custom components have to be added manually.

“引用管理器”对话框Reference Manager dialog box

“引用管理器” 对话框在左侧显示不同类别,具体取决于项目类型:The Reference Manager dialog box shows different categories on the left side, depending on the project type:

  • 程序集,包含“框架”和“扩展”子组 。Assemblies, with the Framework and Extensions subgroups.

  • COM,列出可供引用的所有 COM 组件。COM, lists all COM components that are available for referencing.

  • 解决方案,包含“项目”子组 。Solution, with the Projects subgroup.

  • Windows,包含“核心”和“扩展”子组 。Windows, with the Core and Extensions subgroups. 可以使用“对象浏览器” 浏览 Windows SDK 或扩展 SDK 中的引用。You can explore the references in the Windows SDK or extension SDKs by using the Object Browser.

  • 浏览,包含“最近”子组 。Browse, with the Recent subgroup.

添加引用Add a reference

  1. 在“解决方案资源管理器”中,右键单击“引用”或“依赖项”节点,然后选择“添加引用” 。In Solution Explorer, right-click on the References or Dependencies node and choose Add Reference. 还可以右键单击项目节点,然后选择“添加” > “引用” 。You can also right-click on the project node and select Add > Reference.

    此时将打开“引用管理器” ,并按组列出可用引用。Reference Manager opens and lists the available references by group.

  2. 指定要添加的引用,然后选择“确定” 。Specify the references to add, and then select OK.

“程序集”选项卡Assemblies tab

“程序集”选项卡中列出了可引用的所有 .NET 程序集 。The Assemblies tab lists all .NET assemblies that are available for referencing. “程序集”选项卡不会列出全局程序集缓存 (GAC) 中的任何程序集,因为 GAC 中的程序集是运行时环境的一部分 。The Assemblies tab doesn't list any assemblies from the global assembly cache (GAC) because assemblies in the GAC are part of the run-time environment. 如果某个应用程序包含对在 GAC 中注册的程序集的引用,则在部署或复制该应用程序时,无论“复制本地”设置为何,所引用的程序集都不会与该应用程序一起部署或复制 。If you deploy or copy an application that contains a reference to an assembly that's registered in the GAC, the assembly won't be deployed or copied with the application, regardless of the Copy Local setting. 有关详细信息,请参阅管理项目中的引用For more information, see Manage references in a project.

在手动添加对任何 EnvDTE 命名空间(EnvDTEEnvDTE80EnvDTE90EnvDTE90aEnvDTE100)的引用时,请在“属性”窗口中将引用的“嵌入互操作类型”属性设置为“False” 。When you manually add a reference to any of the EnvDTE namespaces (EnvDTE, EnvDTE80, EnvDTE90, EnvDTE90a, or EnvDTE100), set the Embed Interop Types property of the reference to False in the Properties window. 将此属性设置为“True” 可能会导致生成问题,因为某些 EnvDTE 属性无法嵌入。Setting this property to True can cause build issues because of certain EnvDTE properties that can't be embedded.

所有桌面项目都包含对 mscorlib 的隐式引用 。All desktop projects contain an implicit reference to mscorlib. Visual Basic 项目包含对 Microsoft.VisualBasic 的隐式引用。Visual Basic projects contain an implicit reference to Microsoft.VisualBasic. 即使已将 System.Core 从引用列表删除,所有项目仍包含对其的隐式引用 。All projects contain an implicit reference to System.Core, even if it's removed from the list of references.

如果项目类型不支持程序集,则此选项卡不会显示在“引用管理器”对话框中 。If a project type doesn't support assemblies, the tab won't appear in the Reference Manager dialog box.

“程序集”选项卡包含两个子选项卡 :The Assemblies tab consists of two sub-tabs:

  1. “框架”列出组成目标框架的所有程序集 。Framework lists all assemblies that constitute the targeted framework.

    对于不面向 .NET Core 或通用 Windows 平台的项目,“框架”选项卡中枚举了目标框架中的程序集 。For projects that don't target .NET Core or the Universal Windows Platform, the Framework tab enumerates assemblies from the targeted framework. 用户必须添加应用程序所需的任何引用。The user must add any references that the application requires.

    通用 Windows 项目默认包含对目标框架中所有程序集的引用。Universal Windows projects contain references to all of the assemblies in the targeted framework by default. 在托管项目中,“解决方案资源管理器”的“引用”文件夹下的只读节点表示对整个框架的引用 。In managed projects, a read-only node under the References folder in Solution Explorer indicates the reference to the entire framework. 因此,“框架”选项卡不枚举框架中的任何程序集,而是显示以下消息 :“已引用所有框架程序集。Accordingly, the Framework tab doesn't enumerate any of the assemblies from the framework and instead displays the following message: "All of the Framework assemblies are already referenced. 请使用对象浏览器浏览框架中的引用”。Please use the Object Browser to explore the references in the Framework".

  2. “扩展”列出了组件和控件的外部供应商为扩展目标框架而开发的所有程序集 。Extensions lists all assemblies that external vendors of components and controls have developed to extend the targeted framework. 根据用户应用程序的用途,可能需要这些程序集。Depending on the purpose of the user application, it might need these assemblies.

    “扩展”通过枚举在以下位置注册的程序集来填充 :Extensions is populated by enumerating the assemblies that are registered in the following locations:

    32 位计算机:32-bit machine:

    • HKEY_CURRENT_USER\SOFTWARE\Microsoft\[Target Framework Identifier]\v[Target Framework Version]\AssemblyFoldersEx\[UserComponentName]\@default=[Disk location of assemblies]
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\[Target Framework Identifier]\v[Target Framework Version]\AssemblyFoldersEx\[UserComponentName]\@default=[Disk location of assemblies]

    64 位计算机:64-bit machine:

    • HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\[Target Framework Identifier]\v[Target Framework Version]\AssemblyFoldersEx\[UserComponentName]\@default=[Disk location of assemblies]
    • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\[Target Framework Identifier]\v[Target Framework Version]\AssemblyFoldersEx\[UserComponentName]\@default=[Disk location of assemblies]

    [目标框架标识符]的旧版本And older versions of the [Target Framework Identifier]

    例如,如果项目面向 32 位计算机上的 .NET Framework 4,则“扩展”将枚举在 \Microsoft.NETFramework\v4.0\AssemblyFoldersEx、\Microsoft.NETFramework\v3.5\AssemblyFoldersEx、\Microsoft.NETFramework\v3.0\AssemblyFoldersEx 和 \Microsoft.NETFramework\v2.0\AssemblyFoldersEx 下注册的程序集 。For example, if a project targets .NET Framework 4 on a 32-bit machine, Extensions enumerates assemblies that are registered under \Microsoft.NETFramework\v4.0\AssemblyFoldersEx, \Microsoft.NETFramework\v3.5\AssemblyFoldersEx, \Microsoft.NETFramework\v3.0\AssemblyFoldersEx, and \Microsoft.NETFramework\v2.0\AssemblyFoldersEx.

列表中的某些组件可能不会显示,具体取决于项目的框架版本。Some components in the list may not be shown, depending on the framework version of your project. 在下列条件下,可能会出现这种情况:This can occur under the following conditions:

  • 使用最新框架版本的组件与面向早期版本的项目不兼容。A component that uses a recent framework version is incompatible with a project that targets an earlier version.

    有关如何更改项目的目标框架版本的信息,请参阅框架目标概述For information about how to change the target framework version for a project, see Framework targeting overview.

  • 使用 .NET Framework 4 的组件与面向 .NET Framework 4.5 的项目不兼容。A component that uses .NET Framework 4 is incompatible with a project that targets the .NET Framework 4.5.

应当避免添加对同一解决方案中另一个项目的输出的文件引用,因为这样做可能导致编译错误。You should avoid adding file references to outputs of another project in the same solution, because doing this may cause compilation errors. 而应使用“添加引用” 对话框的“项目” 选项卡来创建项目到项目的引用。Instead, use the Projects tab of the Add Reference dialog box to create project-to-project references. 这样就可以更好地管理在项目中创建的类库,从而更易于进行团队开发。This makes team development easier by enabling better management of the class libraries you create in your projects. 有关详细信息,请参阅有关无效引用的疑难解答For more information, see Troubleshoot broken references.

Note

在 Visual Studio 2015 或更高版本中,如果一个项目的目标框架版本为 .NET Framework 4.5 或更高版本,而另一个项目的目标版本为 .NET Framework 2、3、3.5 或 4.0,则将创建文件引用而不是项目引用。In Visual Studio 2015 or later, a file reference instead of a project reference is created if the target framework version of one project is .NET Framework 4.5 or later, and the target version of the other project is .NET Framework 2, 3, 3.5, or 4.0.

在“添加引用”对话框中显示程序集To display an assembly in the Add Reference dialog box

  • 将程序集移动或复制到下列位置之一:Move or copy the assembly to one of the following locations:

    • 当前项目目录。The current project directory. (可以使用 “浏览” 选项卡查找这些程序集。)(You can find these assemblies by using the Browse tab.)

    • 同一解决方案中的其他项目目录。Other project directories in the same solution. (可以使用“项目” 选项卡查找这些程序集。)(You can find these assemblies by using the Projects tab.)

      - 或 -- or -

  • 设置指定要显示的程序集位置的注册表项:Set a registry key that specifies the location of assemblies to display:

    对于 32 位操作系统,添加以下注册表项之一。For a 32-bit operating system, add one of the following registry keys.

    • [HKEY_CURRENT_USER\SOFTWARE\Microsoft\.NETFramework\<VersionMinimum>\AssemblyFoldersEx\MyAssemblies]@="<AssemblyLocation>"

    • [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\<VersionMinimum>\AssemblyFoldersEx\MyAssemblies]@="<AssemblyLocation>"

    对于 64 位操作系统,在 32 位注册表配置单元中添加以下注册表项之一。For a 64-bit operating system, add one of the following registry keys in a 32-bit registry hive.

    • [HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\<VersionMinimum>\AssemblyFoldersEx\MyAssemblies]@="<AssemblyLocation>"

    • [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\<VersionMinimum>\AssemblyFoldersEx\MyAssemblies]@="<AssemblyLocation>"

    <VersionMinimum> 是所应用的最低框架版本 。<VersionMinimum> is the lowest framework version that applies. 如果 <VersionMinimum> 为 v3.0,则 AssemblyFoldersEx 中指定的文件夹应用于以 3.0 版本及更高版本的 .NET Framework 为目标的项目 。If <VersionMinimum> is v3.0, folders specified in AssemblyFoldersEx apply to projects that target .NET Framework 3.0 and later.

    <AssemblyLocation> 是要在“添加引用”对话框中显示的程序集目录,例如 C:\MyAssemblies 。<AssemblyLocation> is the directory of the assemblies that you want to appear in the Add Reference dialog box, for example, C:\MyAssemblies.

    通过在 HKEY_LOCAL_MACHINE 节点下创建注册表项,所有用户都可以在“添加引用”对话框中的指定位置看到这些程序集 。Creating the registry key under the HKEY_LOCAL_MACHINE node allows all users to see the assemblies in the specified location in the Add Reference dialog box. HKEY_CURRENT_USER 节点下创建注册表项只会影响当前用户的设置。Creating the registry key under the HKEY_CURRENT_USER node affects only the setting for the current user.

    再次打开“添加引用” 对话框。Open the Add Reference dialog box again. 程序集应出现在“.NET” 选项卡中。如果未显示,请确保这些程序集位于指定的 AssemblyLocation 目录中,然后重启 Visual Studio 并重试。The assemblies should appear on the .NET tab. If they do not, make sure that the assemblies are located in the specified AssemblyLocation directory, restart Visual Studio, and try again.

“项目”选项卡Projects tab

在“解决方案”子选项卡中,“项目”选项卡列出当前解决方案中的所有兼容项目 。The Projects tab lists all compatible projects within the current solution, in the Solution sub-tab.

一个项目可以引用面向其他框架版本的另一个项目。A project can reference another project that targets a different framework version. 例如,可以创建一个面向 .NET Framework 4、但引用针对 .NET Framework 2 生成的程序集的项目。For example, you could create a project that targets the .NET Framework 4 but that references an assembly that's been built for the .NET Framework 2. 不过,.NET Framework 2 项目不能引用 .NET Framework 4 项目。However, the .NET Framework 2 project can't reference a .NET Framework 4 project. 有关详细信息,请参阅框架定位概述For more information, see Framework targeting overview.

Note

面向 NET Framework 4 的项目与面向 .NET Framework 4 Client Profile 的项目不兼容。A project that targets the .NET Framework 4 is incompatible with a project that targets the .NET Framework 4 Client Profile.

通用 Windows 选项卡Universal Windows tab

“通用 Windows”选项卡列出特定于运行 Windows 操作系统的平台的所有 SDK 。The Universal Windows tab lists all SDKs that are specific to platforms on which Windows operating systems run. 此选项卡具有两个子组:“核心”与“扩展” 。This tab has two subgroups: Core and Extensions.

“核心”子组Core subgroup

默认情况下,通用 Windows 应用项目具有对通用 Windows SDK 的引用。Universal Windows app projects have a reference to the Universal Windows SDK by default. 因此,“引用管理器”中的“核心”子组不枚举通用 Windows SDK 的任何程序集 。Accordingly, the Core subgroup in the Reference Manager doesn't enumerate any of the assemblies from the Universal Windows SDK.

“扩展”子组Extensions subgroup

“扩展”列出用于扩展目标 Windows 平台的用户 SDK 。Extensions lists the user SDKs that extend the targeted Windows platform.

SDK 是文件集合,Visual Studio 将其视为单个组件。An SDK is a collection of files that Visual Studio treats as a single component. 在“扩展”选项卡中,应用于调用了“引用管理器”对话框的项目的 SDK 将作为单个项列出 。In the Extensions tab, SDKs that apply to the project from which the Reference Manager dialog box was invoked are listed as single entries. 添加到项目时,所有 SDK 内容将由 Visual Studio 使用,这样,用户无需采取任何额外操作即可使用 IntelliSense、工具箱、设计器、对象浏览器、生成、部署、调试和打包中的 SDK 内容。When added to a project, all of the SDK content is consumed by Visual Studio such that the user doesn't need to take any further actions to leverage the SDK contents in IntelliSense, toolbox, designers, Object Browser, build, deployment, debugging, and packaging.

若要了解如何在“扩展”选项卡中显示 SDK,请参阅创建软件开发工具包For information about how to display your SDK in the Extensions tab, see Creating a Software Development Kit.

Note

如果项目引用的 SDK 依赖于另一 SDK,则只有在用户手动添加对另一 SDK 的引用后,Visual Studio 才会使用另一 SDK。If a project references an SDK that depends on another SDK, Visual Studio won't consume the second SDK unless you manually add a reference to the second SDK. 当用户在“扩展”选项卡上选择 SDK 时,“引用管理器”对话框会在结果窗格中列出所有依赖项,从而帮助你找出 SDK 依赖项 。When a user chooses an SDK on the Extensions tab, the Reference Manager dialog box helps you identify SDK dependencies by listing any dependencies in the details pane.

如果项目类型不支持扩展,则此选项卡不会显示在“引用管理器”对话框中 。If a project type doesn't support extensions, this tab doesn't appear in the Reference Manager dialog box.

“COM”选项卡COM tab

“COM”选项卡列出可供引用的所有 COM 组件 。The COM tab lists all COM components that are available for referencing. 如果要添加对包含内部清单的已注册 COM DLL 的引用,请先注销该 DLL。If you want to add a reference to a registered COM DLL that contains an internal manifest, unregister the DLL first. 否则,Visual Studio 会将程序集引用作为 ActiveX 控件而不是本机 DLL 添加。Otherwise, Visual Studio adds the assembly reference as an ActiveX control instead of as a native DLL.

如果项目类型不支持 COM,则此选项卡不会显示在“引用管理器”对话框中 。If a project type doesn't support COM, the tab doesn't appear in the Reference Manager dialog box.

“浏览”按钮Browse button

可以使用“浏览” 按钮浏览查找文件系统中的组件。You can use the Browse button to browse for a component in the file system.

一个项目可以引用面向其他框架版本的组件。A project can reference a component that targets a different framework version. 例如,可以创建一个面向 .NET Framework 4.7、但引用面向 .NET Framework 4 的组件的应用程序。For example, you could create an application that targets .NET Framework 4.7 but references a component that targets .NET Framework 4. 有关详细信息,请参阅框架定位概述For more information, see Framework targeting overview.

避免在同一个解决方案中添加对另一个项目的输出的文件引用,因为这可能导致编译错误。Avoid adding file references to outputs of another project in the same solution, because this tactic may cause compilation errors. 而应使用“引用管理器” 对话框的“解决方案” 选项卡来创建项目到项目的引用。Instead, use the Solution tab of the Reference Manager dialog box to create project-to-project references. 这样就可以更好地管理在项目中创建的类库,从而更易于进行团队开发。This makes team development easier by enabling better management of the class libraries that you create in your projects. 有关详细信息,请参阅有关无效引用的疑难解答For more information, see Troubleshoot broken references.

无法浏览到 SDK 并将其添加到项目。You can't browse to an SDK and add it to your project. 只能浏览到文件(例如程序集或 .winmd)并将其添加到项目 。You can only browse to a file (for example, an assembly or .winmd) and add it to your project.

执行对 WinMD 的文件引用时,预期布局为 <FileName>.winmd、<FileName>.dll 和 <FileName>.pri 文件全部并排放置 。When doing a file reference to a WinMD, the expected layout is that the <FileName>.winmd, <FileName>.dll, and <FileName>.pri files are all placed alongside each other. 如果在以下情况下引用 WinMD,一组不完整的文件将复制到项目输出目录中,从而导致生成和运行时失败。If you reference a WinMD in the following scenarios, an incomplete set of files will be copied into the project output directory and, consequently, build and runtime failures will occur.

  • 本机组件:本机项目将为每个不相交的命名空间集创建一个 WinMD 以及一个包含实现的 DLL。Native component: a native project will create one WinMD for each disjoint set of namespaces and one DLL that consists of the implementation. WinMD 将具有不同的名称。The WinMDs will have disparate names. 引用此本机组件文件时,MSBuild 不会将名称不同的 WinMD 视为一个组件。When referencing this native component file, MSBuild won't recognize that the dissimilarly named WinMDs make one component. 因此,将仅复制名称相同的 <FileName>.dll 和 <FileName>.winmd,并且将发生运行时错误 。Consequently, only the identically named <FileName>.dll and <FileName>.winmd will be copied, and runtime errors will occur. 若要解决此问题,请创建扩展 SDK。To work around this issue, create an extension SDK. 有关详细信息,请参阅创建软件开发工具包For more information, see Create a Software Development Kit.

  • 使用控件:XAML 控件至少包含一个 <FileName>.winmd、一个 <FileName>.dll、一个 <FileName>.pri、一个 <XamlName>.xaml 和一个 <ImageName>.jpg 。Consuming controls: at a minimum, a XAML control consists of a <FileName>.winmd, <FileName>.dll, <FileName>.pri, <XamlName>.xaml, and an <ImageName>.jpg. 生成项目时,与文件引用关联的资源文件不会复制到项目的输出目录中,将仅复制 <FileName>.winmd、<FileName>.dll 和 <FileName>.pri 。When the project is built, the resource files that are associated with the file reference won't get copied into the project's output directory, and only <FileName>.winmd, <FileName>.dll and <FileName>.pri will be copied. 将记录一个生成错误,通知用户缺少 <XamlName>.xaml 和 <ImageName>.jpg 资源 。A build error is logged to inform the user that the resources <XamlName>.xaml and <ImageName>.jpg are missing. 若要成功,用户必须将这些资源文件手动复制到项目输出目录以用于生成和调试/运行时。To succeed, the user will have to manually copy these resource files into the project output directory for build and debugging/runtime. 若要解决此问题,请按照创建软件开发工具包中的步骤创建扩展 SDK,或编辑项目文件,添加以下属性:To work around this issue, either create an extension SDK by following the steps in Create a Software Development Kit or edit the project file to add the following property:

    <PropertyGroup>
       <GenerateLibraryOutput>True</GenerateLibraryOutput>
    </PropertyGroup>
    

    Note

    如果添加属性,生成可能会运行较慢。If you add the property, the build might run slower.

RecentRecent

“程序集”、“COM”、“Windows”和“浏览”均支持“最近”选项卡,此选项卡可枚举最近添加到项目的组件的列表 。Assemblies, COM, Windows, and Browse each support a Recent tab, which enumerates the list of components that were recently added to projects.

“引用管理器” 对话框中的搜索栏在具有焦点的选项卡上运行。The search bar in the Reference Manager dialog box operates over the tab that's in focus. 例如,在“解决方案”选项卡具有焦点时,如果用户在搜索栏中键入“System”,则除非解决方案具有包含“System”的项目名称,否则搜索不会返回任何结果 。For example, if a user types "System" in the search bar while the Solution tab is in focus, the search won't return any results unless the solution consists of a project name that contains "System".

请参阅See also