【问题标题】:C# Roslyn Analyzer via ProjectReference leads to not found error after opening Visual StudioC# Roslyn Analyzer 通过 ProjectReference 导致打开 Visual Studio 后未找到错误
【发布时间】:2022-01-18 01:04:09
【问题描述】:

我为特定的 C# 解决方案编写了一个 Roslyn 分析器,并希望通过 ProjectReference 将分析器添加到解决方案中的所有项目:

<Project>
    <PropertyGroup>
        <LangVersion>8.0</LangVersion>
    </PropertyGroup>
    <ItemGroup>
        <ProjectReference Include="(MSBuildProjectDirectory)\..\..\..\CodeAnalyzers\CodeAnalyzers\CodeAnalyzers.csproj"
         ReferenceOutputAssembly="false" 
         OutputItemType="Analyzer" />
    </ItemGroup>
</Project>

我把它放在 Directory.Build.props 文件中。

只要我在构建解决方案之前手动构建分析器项目,分析器就可以工作。如果我不这样做,每个项目都会收到以下警告:

Warning CS8034  Unable to load Analyzer assembly C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll: Could not find file 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'.
System.IO.FileNotFoundException: Could not find file 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'.
File name: 'C:\Users\xxx\source\repos\XYZ\CodeAnalyzers\CodeAnalyzers\bin\Debug\netstandard1.3\CodeAnalyzers.dll'

我如何告诉 Visual Studio 先构建分析器项目,然后再尝试加载 dll? 之后构建并不能解决问题,我想是因为缓存。

在“项目构建顺序”对话框中,分析器项目位于顶部,而在配置管理器中,构建的复选框已设置。

【问题讨论】:

  • 如果像这样添加ProjectReference,被引用的项目不会有对自身的引用吗?您在构建过程中是否看到任何其他警告?

标签: c# .net visual-studio roslyn roslyn-code-analysis


【解决方案1】:

您的分析器项目似乎与您的特定 C# 解决方案位于同一解决方案中。 更直接的方法是从您的单独分析器解决方案创建 Nuget 包(在 VS“带有代码修复的分析器(.NET 标准)”中使用模板),然后使用 PackageReference 将您的分析器包含在您的解决方案中。 例如,放入 props 文件或 csproj 文件中:

<ItemGroup>
  <PackageReference Include="YourCustomAnalyzerId" Version="x.x.x" />
</ItemGroup>

您将在所有项目中获得分析器,一切正常。然后您的分析器将列在 Dependencies/Analyzers 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多