如何在 Visual Studio Code 中添加程序集引用? [英] How do I add assembly references in Visual Studio Code?
问题描述
所以我现在在 C# 中处理我的第一个项目时遇到了两次类似的问题.尝试添加 using System.Data;
或 using System.Timers;
时,出现以下错误:
命名空间System"中不存在类型或命名空间名称x"(您是否缺少程序集引用?).
我尝试开始一个新项目并运行 restore
以查看我是否不小心删除了依赖项中的某些内容,但是在生成新项目时我仍然收到相同的错误.我试图研究这个问题并看到了关于解决方案资源管理器"的答案,但据我所知,在 Visual Studio Code 1.8 中似乎没有这个名称的功能.
谁能为我指出如何使这些工作正确的方向,也许是通过手动添加到依赖项中?
.csproj 项目文件
以下主题适用于 .csproj
项目文件和:.NET Core 1.x SDK、.NET Core 2.x SDK
向项目文件添加包引用.
dotnet 添加包
示例
将 Newtonsoft.Json
NuGet 包添加到项目中:
dotnet 添加包 Newtonsoft.Json
.json 项目文件
以下主题适用于.json
项目文件:
本指南将引导您完成在 Visual Studio Code 中添加任何程序集引用的过程.在此示例中,我们将程序集引用 System.Data.SqlClient 添加到 .NET Core C# 控制台应用程序中.
注意
- 在第 6 步,输入所需的程序集引用.
- 一些程序集引用适用于 .NET Framework,它会给你错误.
- OleDb 在 .NET Core 中不可用,可能是因为它不是跨平台的.
先决条件
- 安装
- 在命令面板框中,输入nu
点击NuGet 包管理器:添加包
输入包过滤器,例如system.data(在此处输入您的程序集参考)
- 按 Enter
- 点击System.Data.SqlClient
- 弹出如下提示
- 点击恢复
- 弹出以下输出面板
- 在资源管理器面板中,点击project.json将其打开
- 在编辑器面板中,它显示了添加到project.json文件中的程序集引用
- 程序集参考,System.Data.SqlClient在Program.cs 中使用
So I've come across a similar issue twice now while working on my first project in C#. When trying to add either
using System.Data;
orusing 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?).
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. 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.Can anyone point me in the right direction for how to get these working, perhaps by manually adding into the dependencies?
解决方案.csproj Project file
The following topic applies to
.csproj
project file and : .NET Core 1.x SDK, .NET Core 2.x SDKAdds a package reference to a project file.
dotnet add package
Example
Add
Newtonsoft.Json
NuGet package to a project:dotnet add package Newtonsoft.Json
.json Project file
The following topic applies to
.json
project file:This guide walks you through the process of adding any assembly reference in Visual Studio Code. In this example, we are adding the assembly reference System.Data.SqlClient into .NET Core C# console application.
Note
- At step #6, enter the assembly reference that you want.
- Some assembly reference is applicable to .NET Framework and it will gives you error(s).
- OleDb is not available in .NET Core, probably because it's not cross platform.
Prerequisites
- Install Visual Studio Code
- Install .NET Core SDK (Preview 2 version)
- Install NuGet Package Manager from the Visual Studio Code Extension Marketplace
- Install C# extension from Visual Studio Code Extension Marketplace
Steps
- Launch Visual Studio Code
- Open your project folder
- Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command Palette
- In Command Palette box, type nu
Click on NuGet Package Manager: Add Package
Enter package filter e.g. system.data (Enter your assembly reference here)
- Press Enter
- Click on System.Data.SqlClient
- The following prompt pops up
- Click on Restore
- The following Output panel pops up
- In the Explorer panel, click on project.json to open it
- In the Editor panel, it shows the assembly reference added into project.json file
- Assembly reference, System.Data.SqlClient used in Program.cs
这篇关于如何在 Visual Studio Code 中添加程序集引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!