磁盘上有物理文件的源代码生成器导致歧义

Source generator with physical files on disk causing ambiguity

我正在开发一个项目,我想根据 JSON 定义一组异常的文件生成 cs 类。我研究了不同的选项,现在尝试使用源代码生成器。在我的项目中,我想生成文件并将它们放在层次结构中的 JSON 文件下,而不是在分析器下(默认行为)。为此,我使用了:

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
    <CompilerGeneratedFilesOutputPath>GeneratedFiles</CompilerGeneratedFilesOutputPath>
  </PropertyGroup>

我的项目现在面临两个问题,首先是每当我尝试使用 Newtonsoft 将 json 文件(附加文件)反序列化到我自己的模型时,我都会收到此错误。

CSC : warning CS8785: Generator 'BaseExceptionGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'

第二个问题,当我在 generated 文件夹下构建和生成文件时,我将拥有相同的文件(一个在 analyzers 下,一个在这个文件夹下),每当我尝试调用 generated 的方法时,这会导致歧义代码。

Severity    Code    Description Project File    Line    Suppression State
Error   CS0111  Type 'Hello' already defines a member called 'Display' with the same parameter types    SourceGeneratorTest ..\SourceGeneratorTest\SourceGeneratorTest\Generators\SoruceGenerator.BaseExceptionGenerator\BaseException.cs   9   Active

代码库:https://github.com/KhaledSamir/SourceGeneratorTest

我是不是做错了什么?

对于遇到相同问题的任何人,我发现问题的解决方案是:

1- CSC : warning CS8785: Generator 'BaseExceptionGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.'

遵循 cookbook 并修复了它。

2- Error CS0111 Type 'Hello' already defines a member called 'Display' with the same parameter types SourceGeneratorTest ..\SourceGeneratorTest\SourceGeneratorTest\Generators\SoruceGenerator.BaseExceptionGenerator\BaseException.cs 9 Active

不得不将其从编译中删除,检查这个 link。基本上做了类似的事情:

<ItemGroup>
    <Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>