Visual Studio 代码中如何将外部程序集 (.dll) 添加到 .NET Core 2.0

How to add external assembly (.dll) to .NET Core 2.0 on Visual Studio Code

我在 Visual Studio Code 上向我的 .NET Core 2.0 控制台应用程序添加外部程序集 (.dll) 时遇到了一些问题,因为有关于如何做到这一点的 none 文档很少。

Microsoft 提供了关于如何将 NuGet Packages 和项目添加到项目引用的 very good tutorial,但没有关于如何添加外部 dlls.

的信息

经过一些研究,我设法让它工作了。

  1. 打开您的 .csproj 文件

  2. </PropertyGroup>标签下,添加

<ItemGroup>
  <Reference Include="Your dll file name">
    <HintPath>Your dll file name.dll</HintPath>
    <SpecificVersion>False</SpecificVersion> 
    <!-- You may set it to true if your dll has a specific version -->
  </Reference>
</ItemGroup>

  1. dll 移动到项目的 root 文件夹(Program.cs 所在的文件夹)

  2. 使用console/terminal导航到项目的root文件夹并执行dotnet restore导入所有引用

  3. 然后,执行dotnet run

  4. 不要从您的根文件夹中删除 dll。如果这样做,您将收到以下错误:

error CS0246: The type or namespace name 'Your dll File' could not be found (are you missing a using directive or an assembly reference?)

右键单击项目 添加现有项目 > select 路径到 .dll 在项目中添加dll后,右击.dll

build-action = Content, Copy-to-output-dir = Always/ or if newer

Visual Studio 社区 2019 [v16.8.4]

  1. 右键单击您的 aspnetcore 项目
  2. Project Reference... > 浏览 > Browse...
  3. 找到并 Add 您的 DLL 文件