.net 程序集绑定引用问题



我有一个 .NetStandard 2.0 项目称为 AB,它引用了项目 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:无法加载文件或程序集 'System.ComponentModel.Annotations, version=4.2.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。

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

我该如何解决这个问题?

请注意,只要使用者是 .Net 使用者(标准或框架),就没有问题。当我们的非 .Net 使用者开始使用我们的库时,就会发现这个问题。

检查框架是否引用项目 AB 的 csproj 中的特定版本:

<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>

最新更新