c# - .Net 程序集绑定(bind)引用问题

标签 c# .net-framework-version .net-standard-2.0

我有一个名为 AB 的 .NetStandard 2.0 项目,它引用了项目 A 和 B。 项目 A 是一个 .Net Standard2.0,它需要来自 System.ComponentModel.DataAnnotation System.ComponentModel.Annotations,Version=4.2.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a。

然而,项目 B 是一个 .NetFramework4.6.1 项目,它还需要 System.ComponentModel.DataAnnotation,它是从 System.ComponentModel.DataAnnotations,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35。

构建 AB 程序集时,它仅发出一个 System.ComponentModel.DataAnnotation.dll,恰好来自项目 B。

因此,在使用 AB 程序集时,无法实例化项目 A 中的对象并出现错误:

System.IO.FileNotFoundException: Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

但是项目 B 中的对象正常工作。

我该如何解决这个问题?

请注意,只要消费者是 .Net 消费者(Standard 或 Framework)就没有问题。当我们的非 .Net 用户开始使用我们的库时,问题就被发现了。

最佳答案

检查 framework to reference a specific version in the csproj项目AB:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>

关于c# - .Net 程序集绑定(bind)引用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477341/

相关文章:

c# - map 控制 Gmap.net 崩溃

c# - 无法加载文件或程序集“System.Security.Cryptography.Xml”

c# - 如何临时暂停实时数据图表的绘制更新

c# - 如何启动单独的进程

c# - 无法解析 .Net Core 3.1 中的 System.Windows.Forms.dll

c# - 无法安装 .NET 框架,因为 Windows 认为安装了更新版本

c# - 从 .NET Standard 2.0 库读取 app.config

c# - 类型 [Type] 存在于 [Assembly1] 和 [netstandard 2.0 assembly] 中

c# - Configuration.GetSection 返回空值

c# - 如何使 WPF 应用程序在屏幕上居中?