You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.8引用.NET Standard 2.0类库遇System.Runtime错误求助

解决.NET Framework 4.8类库引用.NET Standard 2.0类库的运行时错误

问题背景

ClassLibrary1(.NET Framework 4.8)引用ClassLibrary2(.NET Standard 2.0)时,出现运行时错误:

Could not load file or assembly 'System.Runtime, Version=6.0.0.0,Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 
The system cannot find the file specified.":"System.Runtime, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

尝试添加System.Runtime 6.0.0引用后,又出现编译冲突:

The type 'xyztype' found in both 'mscorelib' and 'System.Runtime v6.0.0'

解决方案

1. 替换兼容包,移除冲突依赖

卸载之前安装的NETStandard.Lib和System.Runtime包,为ClassLibrary1安装Microsoft.NETFramework.Compatibility NuGet包。这个官方兼容层会自动处理.NET Framework与.NET Standard的依赖映射,避免手动引用高版本核心库导致的类型冲突。

2. 配置程序集绑定重定向(必要时)

如果安装兼容包后仍有加载错误,需在最终运行的应用程序的App.config或Web.config中添加绑定重定向(类库自身的配置文件不生效,必须由调用它的应用程序配置):

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.2.2.0" />
      </dependentAssembly>
      <!-- 若出现其他核心库加载错误,可参照格式添加对应重定向 -->
    </assemblyBinding>
  </runtime>
</configuration>

注:newVersion对应.NET Framework 4.8中System.Runtime的实际版本,通常为4.2.2.0,可通过查看ClassLibrary1的引用列表确认。

3. 检查ClassLibrary2的依赖

确保ClassLibrary2未引用.NET Core/.NET 5+专属的NuGet包(如仅支持net5.0+的包)。这类包无法在.NET Framework 4.8环境运行,需替换为兼容.NET Standard 2.0和.NET Framework 4.8的包版本。

4. 清理重建解决方案

  • 右键解决方案,选择「清理解决方案」
  • 删除所有项目的bin和obj文件夹
  • 重新生成整个解决方案

内容的提问来源于stack exchange,提问作者Smit Rathod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 16:39:21