我們最近一直在我們的產品中測驗 Source 生成器,到目前為止效果非常好。但是,我的一位使用 VS 2019 的同事在讓專案與分析器一起運行時遇到問題,但它似乎在 VS 2022 上運行良好。
我在 2019 年收到此警告訊息:
Warning CS8032 An instance of analyzer
...Generator cannot be created from analyzer\...\analyzers\dotnet\cs\Analyzer.dll:
Could not load file or assembly
'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
or one of its dependencies.
The system cannot find the file specified
我的生成器專案如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>...</Authors>
<Description>...</Description>
<Copyright>$(Authors)</Copyright>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
<Deterministic>false</Deterministic>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Version />
<PackageTags>Analyzer</PackageTags>
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>
<ItemGroup>
<None Remove="config.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="config.xml" CopyToOutputDirectory="Always" Pack="false"/>
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
</ItemGroup>
</Project>
和運行后生成的nuspec dotnet pack:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>...Analyzer</id>
<version>...</version>
<authors>...</authors>
<description>...</description>
<copyright>...</copyright>
<tags>Analyzer</tags>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.CodeAnalysis.CSharp" version="4.1.0" exclude="Build,Analyzers" />
<dependency id="Microsoft.CodeAnalysis.Common" version="4.1.0" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
</package>
只有我們的分析器 dll 在analyzers\dotnet\cs\. 消費者專案如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net472</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<deterministic>false</deterministic>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
這一切看起來都是正確的,并在消費者專案中添加了正確的 nuget 依賴項,所以我不確定我還能做錯什么。這是 2019 年和 2022 年不同的 .NET 目標問題嗎?
到目前為止,我已經:
- 清除了 nuget 快取
- 洗掉了 .vs 檔案夾
- 重啟VS 2019
而且沒有運氣。任何幫助表示贊賞。
uj5u.com熱心網友回復:
如果您在生成器中依賴 4.1 版的 Roslyn,這意味著您的最低 VS 版本將是 VS2022 17.1。如果要支持 VS2019,需要參考一些 3.* 版本的 Roslyn。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/481574.html
上一篇:如何解決錯誤:運算子“==”不能應用于“方法組”和“int”型別的運算元
下一篇:如何制作游戲物件的副本?
