简体   繁体   English

如何在 Visual Studio Code 中添加程序集引用?

[英]How do I add assembly references in Visual Studio Code?

So I've come across a similar issue twice now while working on my first project in C#.所以我现在在 C# 中处理我的第一个项目时遇到了两次类似的问题。 When trying to add either using System.Data;尝试using System.Data;添加时using System.Data; or using System.Timers;using System.Timers; , I get the following error: ,我收到以下错误:

The type or namespace name 'x' doesn't exist in the namespace 'System' (are you missing an assembly reference?).命名空间“System”中不存在类型或命名空间名称“x”(您是否缺少程序集引用?)。

I have tried beginning a new project and running restore to see if I had accidentally removed something in the dependencies, but upon generating a new project I still receive the same error.我尝试开始一个新项目并运行restore以查看我是否不小心删除了依赖项中的某些内容,但是在生成新项目时我仍然收到相同的错误。 I have tried to research the question and have seen answers referring to the 'solutions explorer', but as far as I can see there doesn't seem to be such a feature by this name in Visual Studio Code 1.8.我试图研究这个问题,并看到了关于“解决方案资源管理器”的答案,但据我所知,在 Visual Studio Code 1.8 中似乎没有这个名称的功能。

Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?任何人都可以指出我如何让​​这些工作的正确方向,也许是通过手动添加到依赖项中?

.csproj Project file .csproj 项目文件

The following topic applies to .csproj project file and : .NET Core 1.x SDK, .NET Core 2.x SDK以下主题适用于.csproj项目文件和:.NET Core 1.x SDK、.NET Core 2.x SDK

Adds a package reference to a project file.向项目文件添加包引用。

dotnet add package

Example例子

Add Newtonsoft.Json NuGet package to a project:Newtonsoft.Json NuGet 包添加到项目中:

dotnet add package Newtonsoft.Json

.json Project file .json 项目文件

The following topic applies to .json project file:以下主题适用于.json项目文件:

This guide walks you through the process of adding any assembly reference in Visual Studio Code.本指南将引导您完成在 Visual Studio Code 中添加任何程序集引用的过程。 In this example, we are adding the assembly reference System.Data.SqlClient into .NET Core C# console application.在此示例中,我们将程序集引用System.Data.SqlClient添加到 .NET Core C# 控制台应用程序中。

Note笔记

  • At step #6, enter the assembly reference that you want.在第 6 步,输入所需的程序集引用。
  • Some assembly reference is applicable to .NET Framework and it will gives you error(s).一些程序集引用适用于 .NET Framework,它会给你错误。
  • OleDb is not available in .NET Core, probably because it's not cross platform. OleDb 在 .NET Core 中不可用,可能是因为它不是跨平台的。

Prerequisites先决条件

  1. Install Visual Studio Code安装Visual Studio 代码
  2. Install .NET Core SDK (Preview 2 version)安装.NET Core SDK (预览 2 版本)
  3. Install NuGet Package Manager from the Visual Studio Code Extension Marketplace从 Visual Studio Code 扩展市场安装NuGet 包管理器
  4. Install C# extension from Visual Studio Code Extension Marketplace从 Visual Studio Code 扩展市场安装C# 扩展

Steps脚步

  1. Launch Visual Studio Code启动 Visual Studio 代码
  2. Open your project folder打开你的项目文件夹
  3. Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command PaletteF1Ctrl+Shift+PMenu Bar > View > Command Palette启动 VS Code Command Palette

在此处输入图片说明

  1. In Command Palette box, type nu命令面板框中,键入nu

在此处输入图片说明

  1. Click on NuGet Package Manager: Add Package单击NuGet 包管理器:添加包

  2. Enter package filter eg system.data ( Enter your assembly reference here )输入包过滤器,例如system.data在此处输入您的程序集参考

在此处输入图片说明

  1. Press Enter按 Enter
  2. Click on System.Data.SqlClient单击System.Data.SqlClient

在此处输入图片说明

  1. The following prompt pops up弹出如下提示

在此处输入图片说明

  1. Click on Restore点击恢复

在此处输入图片说明

  1. The following Output panel pops up弹出如下输出面板

在此处输入图片说明

  1. In the Explorer panel , click on project.json to open itExplorer 面板中,单击project.json将其打开

在此处输入图片说明

  1. In the Editor panel , it shows the assembly reference added into project.json fileEditor 面板中,它显示了添加到project.json文件中的程序集引用

在此处输入图片说明

  1. Assembly reference, System.Data.SqlClient used in Program.cs程序集参考, Program.cs 中使用的System.Data.SqlClient

在此处输入图片说明

Use the command dotnet add package to add a package reference to your project.使用命令dotnet add package向项目添加包引用。 For example: dotnet add package Newtonsoft.Json , which adds the package reference to the *.csproj project file:例如: dotnet add package Newtonsoft.Json ,它添加了对*.csproj项目文件的包引用:

<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

and now you can run the command dotnet restore to restores the dependencies of your project.现在您可以运行命令dotnet restore来恢复项目的依赖项。

Reference: dotnet add package参考: dotnet 添加包

drag the dll file and drop it into the bin folder将dll文件拖放到bin文件夹中

将dll文件拖放到bin文件夹中

Above answer from ikolim doesnt work as indicated by someone else too, there is no, Nuget: Install/Reference command.上面来自 ikolim 的答案也没有按照其他人的指示工作,没有,Nuget: Install/Reference 命令。 There is only Add Package!只有添加包! So the answer in the below link solved my problem.所以下面链接中的答案解决了我的问题。 Manually editing the Myproject.csproj file.手动编辑 Myproject.csproj 文件。

Duplicate of this thread 此线程的重复

In case of extisting .dll reference, Right click project Add existing item > select path to .dll After added dll in project,right click .dll如果存在 .dll 引用,请右键单击项目 添加现有项目 > 选择 .dll 的路径 在项目中添加 dll 后,右键单击 .dll

build-action = Content, Copy-to-output-dir = Always/ or if newer build-action = Content, Copy-to-output-dir = Always/ 或者如果更新

I've stored the files in a project folder named "dlls" and added the reference files in my .csproj file like this:我将文件存储在名为“dlls”的项目文件夹中,并在我的 .csproj 文件中添加了参考文件,如下所示:

 <ItemGroup> <Reference Include="Microsoft.Office.Client.Policy.Portable"> <HintPath>dlls\\Microsoft.Office.Client.Policy.Portable.dll</HintPath> </Reference> <Reference Include="Microsoft.Office.Client.TranslationServices.Portable"> <HintPath>dlls\\Microsoft.Office.Client.TranslationServices.Portable.dll</HintPath> </Reference> </ItemGroup>

So I've come across a similar issue twice now while working on my first project in C#.因此,在使用C#进行第一个项目时,我遇到了两次类似的问题。 When trying to add either using System.Data;尝试using System.Data;添加时using System.Data; or using System.Timers;using System.Timers; , I get the following error: ,出现以下错误:

The type or namespace name 'x' doesn't exist in the namespace 'System' (are you missing an assembly reference?).类型或名称空间名称“ x”在名称空间“系统”中不存在(您是否缺少程序集引用?)。

I have tried beginning a new project and running restore to see if I had accidentally removed something in the dependencies, but upon generating a new project I still receive the same error.我尝试开始一个新项目并运行restore以查看是否意外删除了依赖项中的某些内容,但是在生成新项目时,我仍然收到相同的错误。 I have tried to research the question and have seen answers referring to the 'solutions explorer', but as far as I can see there doesn't seem to be such a feature by this name in Visual Studio Code 1.8.我试图研究该问题,并看到了涉及“解决方案资源管理器”的答案,但是据我所知,Visual Studio Code 1.8中似乎没有这个名称的功能。

Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?有人可以通过手动添加依赖项为我指出如何使它们正常工作的正确方向吗?

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

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