【问题标题】:How to add built-in assembly from .Net Core to a C# project in VS Code如何在 VS Code 中将内置程序集从 .Net Core 添加到 C# 项目
【发布时间】:2020-01-09 15:56:31
【问题描述】:

如何在 VS Code 中将 .Net Core 内置程序集(例如 System.Windows.Forms)添加到 C# 项目?

这可以通过使用参考管理器在 Visual Studio 中轻松完成。但是,我被 VS Code 卡住了。有关于如何从 NuGet 包添加程序集的教程,但我找不到关于添加 .Net Core 程序集的教程。

更新: 我尝试了 Nsevens 所说的有以下参考。

<ItemGroup>
     <Reference Include="System.Windows.Forms">
      <HintPath>thePath\System.Windows.Forms.dll</HintPath>
     </Reference>
</ItemGroup>

编辑器没有发现问题,可以编译成功。

但是,在调试模式下,程序在开始调试后立即消失。调试控制台说

程序 '[(some number)] MyProgram.dll' 已退出,代码为 0 (0x0)。

如果直接运行exe会失败,控制台提示

无法加载文件或程序集“System.Windows.Forms,版本=4.0.0.0”。不应加载引用程序集以供执行。它们只能在 Reflection-only loader 上下文中加载。 (0x80131058)

未处理的异常。 System.BadImageFormatException:无法加载文件或程序集“System.Windows.Forms,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089”。不应加载引用程序集以供执行。它们只能在 Reflection-only loader 上下文中加载。 (0x80131058) 文件名:'System.Windows.Forms,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089' ---> System.BadImageFormatException:无法加载参考程序集以执行。 在 MyProgram.Program.Main(String[] args)

【问题讨论】:

  • 你找到解决办法了吗?

标签: c# visual-studio-code .net-assembly


【解决方案1】:

将以下代码添加到项目文件(.csproj)中

<ItemGroup>
  <Reference Include="MyAssembly">
    <HintPath>path\to\MyAssembly.dll</HintPath>
  </Reference>
</ItemGroup>

(来自:https://medium.com/@tonerdo/referencing-a-net-dll-directly-using-the-net-core-toolchain-16f0af46a4dc

【讨论】:

  • 谢谢。我试过这个,但仍然失败。在调试模式下,程序在开始调试后立即消失。如果 exe 直接运行,它将失败并且控制台显示无法加载文件或程序集“System.Windows.Forms,版本=4.0.0.0”。不应加载引用程序集以供执行。它们只能在 Reflection-only loader 上下文中加载。 (0x80131058)
猜你喜欢
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
相关资源
最近更新 更多