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

.NET5+Roslyn运行时编译报错:无法加载Microsoft.CodeAnalysis程序集

问题描述

我尝试使用Roslyn编译器在运行时编译源代码,编写了如下代码:

public static void compile()
{
    SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
}

这段代码功能有限,但能体现我的问题:每当执行compile方法时,都会抛出异常:

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

我目前使用VS2019和.NET5(这应该是VS2019支持的最新.NET版本)。通过NuGet包管理器,已在项目中引入以下包:

  • Microsoft.CodeAnalysis.Common V4.9.2
  • Microsoft.CodeAnalysis.CSharp V4.9.2

项目文件配置如下:

<ItemGroup>
  <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.9.2" />
  <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
</ItemGroup>

编译时还会收到以下警告:

1>C:\Users\werber\.nuget\packages\system.text.encoding.codepages\8.0.0\buildTransitive\netcoreapp2.0\System.Text.Encoding.CodePages.targets(4,5): warning : System.Text.Encoding.CodePages 8.0.0 doesn't support net5.0 and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
1>C:\Users\werber\.nuget\packages\system.collections.immutable\8.0.0\buildTransitive\netcoreapp2.0\System.Collections.Immutable.targets(4,5): warning : System.Collections.Immutable 8.0.0 doesn't support net5.0 and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
1>C:\Users\werber\.nuget\packages\system.reflection.metadata\8.0.0\buildTransitive\netcoreapp2.0\System.Reflection.Metadata.targets(4,5): warning : System.Reflection.Metadata 8.0.0 doesn't support net5.0 and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.

请问该如何解决这个问题?


解决方案

核心原因

Roslyn 4.9.x版本的包是针对.NET 6及以上版本开发的,它依赖的System.Collections.Immutable、System.Reflection.Metadata等基础包已升级到8.0.0版本,而这些8.0.0版本的包不再支持.NET 5,导致编译时出现兼容警告,运行时因找不到匹配版本的程序集抛出加载异常。


方案一:升级目标框架到.NET 6(推荐)

VS2019的最新版本完全支持.NET 6,升级后可以完美兼容Roslyn 4.9.x的依赖,同时消除所有编译警告:

  1. 打开项目文件(.csproj),将<TargetFramework>net5.0</TargetFramework>修改为:
    <TargetFramework>net6.0</TargetFramework>
    
  2. 在Visual Studio中右键项目 → 管理NuGet程序包 → 点击还原按钮,重新加载依赖包。
  3. 清理项目后重新编译运行。

方案二:降级Roslyn包到兼容.NET 5的版本

如果必须保留.NET 5,需要将Roslyn包降级到最后一批支持.NET 5的版本(4.4.x系列):

  1. 修改项目文件中的NuGet引用版本:
    <ItemGroup>
      <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.4.0" />
      <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
    </ItemGroup>
    
  2. 清理本地NuGet缓存(可以通过dotnet nuget locals all --clear命令执行)。
  3. 重新还原NuGet包,编译运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 05:01:04