且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 .NET Core 项目中引用外部 DLL

更新时间:2023-02-16 09:52:52

.Net Core 2 支持直接引用外部 .dll(例如 Net Standard 库,classic.Net 框架库).您可以通过 Visual Studio UI 完成:右键单击 Dependencies->Add reference->Browse 并选择您的外部 .dll.

.Net Core 2 supports a direct reference to external .dll (e.g. Net Standard libraries, classic .Net Framework libraries). You can do it through Visual Studio UI: right click on Dependencies->Add reference->Browse and select your external .dll.

或者,您可以编辑 .csproj 文件:

Alternatively, you can edit .csproj file:

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

您可能会遇到以下错误:

You can face with the following error:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly

然后只需删除 in 文件夹并重建项目.它应该可以解决问题.

then just remove in folder an rebuild the project. It should fix the issue.

Net Core 2.0 支持 .Net Standard 2.0.Net Standard 2.0 提供了一个 兼容模式 连接.Net Core(.Net Standard) 和.NET Framework.它可以重定向引用,例如到 System.Int32mscorlib.dll(Net.Framework) 到 System.Runtime.dll(Net.Core).但是,即使您的网络核心应用程序成功编译并依赖于外部 dll,如果外部库使用了任何 .Net Standard 没有的 API,您在运行时仍可能存在兼容性问题.

Net Core 2.0 supports .Net Standard 2.0. Net Standard 2.0 provides a compatibility mode to connect .Net Core(.Net Standard) and .NET Framework. It can redirect references e.g. to System.Int32 from mscorlib.dll(Net. Framework) to System.Runtime.dll(Net. Core). But even if your net core app is successfully compiled with dependency on external dll you may still have issues with compatibility during runtime if there is any API used by external library which .Net Standard doesn’t have.