.NET 7预览版WinForms项目出现分析器未找到CS8032/CS8033警告
我创建了一个测试项目用于体验最新版.NET 7(preview-5)的正则表达式代码生成功能,测试项目运行正常。将相同改动应用到一个现有非生产环境的个人效率工具项目上时,莫名出现如下警告:
CS8032 An instance of analyzer Microsoft.CodeAnalysis.CSharp.ValidateFormatString.CSharpValidateFormatStringDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\7.0.100-preview.5.22307.18\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. Das System kann die angegebene Datei nicht finden.. CS8032 An instance of analyzer Microsoft.CodeAnalysis.CSharp.UseUTF8StringLiteral.UseUTF8StringLiteralDiagnosticAnalyzer cannot be created from C:\Program Files\dotnet\sdk\7.0.100-preview.5.22307.18\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll : Exception has been thrown by the target of an invocation.. CS8033 The assembly C:\Program Files\dotnet\sdk\7.0.100-preview.5.22307.18\Sdks\Microsoft.NET.Sdk\codestyle\cs\Microsoft.CodeAnalysis.CodeStyle.dll does not contain any analyzers.
报错中的德语语句 Das System kann die angegebene Datei nicht finden.. 大意为“系统找不到指定文件”,不同语言环境下的错误提示可能存在差异。
前两类CS8032警告累计超过200条,仅涉及的分析器类型不同。
正常运行的测试项目是控制台应用,出问题的是Windows Forms应用,暂不确定该项目类型差异是否与问题相关。以下是问题项目的.csproj相关配置片段:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>net7.0-windows</TargetFramework> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> <ImplicitUsings>disable</ImplicitUsings> <AnalysisLevel>latest-all</AnalysisLevel> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <LangVersion>preview</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Svg" Version="3.4.2" /> </ItemGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <WarningLevel>1</WarningLevel> <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <WarningLevel>1</WarningLevel> <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> </PropertyGroup> </Project>
我曾尝试注释掉Svg依赖及相关调用代码,问题没有明显改善。
作为对比,以下是正常运行的测试项目的.csproj配置:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <LangVersion>preview</LangVersion> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> </Project>
我已确认报错路径下的相关DLL文件确实存在,始终无法定位分析器加载失败的原因。我怀疑问题与报错中的Version=4.3.0.0版本标识有关,但不清楚该版本对应什么组件、正确版本号是什么,也不知道该在什么位置配置相关版本。
我曾查到解决方案建议配置多目标框架<TargetFrameworks>net7.0-windows;netstandard-2.0</TargetFrameworks>,尝试后没有效果。
我已检索数小时,暂未找到其他用户出现同类问题,类似问题的公开解决方案均无法解决我的问题。
补充编辑(2022-12-17):将Visual Studio更新到对应新版本后,该问题自动消失。
内容的提问来源于stack exchange,提问作者Bolpat

