如何在 Visual Studio Code 中添加程序集引用? [英] How do I add assembly references in Visual Studio Code?

查看:56
本文介绍了如何在 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 中不可用,可能是因为它不是跨平台的.

先决条件

  1. 安装

    1. 命令面板框中,输入nu

    1. 点击NuGet 包管理器:添加包

    2. 输入包过滤器,例如system.data(在此处输入您的程序集参考)

    1. 按 Enter
    2. 点击System.Data.SqlClient

    1. 弹出如下提示

    1. 点击恢复

    1. 弹出以下输出面板

    1. 资源管理器面板中,点击project.json将其打开

    1. 编辑器面板中,它显示了添加到project.json文件中的程序集引用

    1. 程序集参考,System.Data.SqlClientProgram.cs
    2. 中使用

    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; or 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?).

    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 SDK

    Adds 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

    1. Install Visual Studio Code
    2. Install .NET Core SDK (Preview 2 version)
    3. Install NuGet Package Manager from the Visual Studio Code Extension Marketplace
    4. Install C# extension from Visual Studio Code Extension Marketplace

    Steps

    1. Launch Visual Studio Code
    2. Open your project folder
    3. Launch VS Code Command Palette by pressing F1 or Ctrl+Shift+P or Menu Bar > View > Command Palette

    1. In Command Palette box, type nu

    1. Click on NuGet Package Manager: Add Package

    2. Enter package filter e.g. system.data (Enter your assembly reference here)

    1. Press Enter
    2. Click on 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 it

    1. In the Editor panel, it shows the assembly reference added into project.json file

    1. Assembly reference, System.Data.SqlClient used in Program.cs

    这篇关于如何在 Visual Studio Code 中添加程序集引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆