我创建了一个玩具项目来检查最新的 .NET 7(预览版 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..翻译为“系统找不到指定的文件”,但这是 DeepL 翻译,实际的英文错误消息可能有所不同。
在前两者中,有 200 多个志同道合的人,但分析者不同。
玩具项目是一个控制台应用程序,有问题的项目是一个 Windows 窗体应用程序(如果这在某种程度上相关的话)。我相信这是该协议的相关部分.csproj
file:
<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):更新VS到某个版本后问题消失。