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

查看:1025
本文介绍了如何在Visual Studio Code中添加程序集引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我用C#进行第一个项目时,我遇到了两次类似的问题。尝试使用System.Data添加添加时,出现以下错误:


名称空间 System中不存在类型或名称空间名称 x(您是否缺少程序集引用?)。 / p>

我尝试开始一个新项目并运行 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. 资源管理器面板 中,单击 project.json 将其打开


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


    1. Program.cs 中使用的程序集引用 System.Data.SqlClient em>


    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天全站免登陆