[英]Source Generator not seeing classes in same project
I have a Source Generator, it looks like this:我有一个源生成器,它看起来像这样:
public class Generator : ISourceGenerator
{
public Generator()
{
// Uncomment if you want to debug this
Debugger.Launch();
}
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new MySyntaxReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
var syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver;
var responseMessageClasses = syntaxReceiver.ResponseMessageClasses;
// do stuff to responseMessageClasses
}
}
internal class MySyntaxReceiver : ISyntaxReceiver
{
public List<ClassDeclarationSyntax> ResponseMessageClasses { get; private set; } = new List<ClassDeclarationSyntax>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is ClassDeclarationSyntax cds &&
cds.Identifier.ValueText.EndsWith("ResponseMessage", StringComparison.OrdinalIgnoreCase))
{
ResponseMessageClasses.Add(cds);
}
}
}
In the same project, I have a folder of simple classes of which the name all ends with "ResponseMessage".在同一个项目中,我有一个简单类的文件夹,其名称都以“ResponseMessage”结尾。 These classes only have simple types like string, int and bool as properties.这些类只有简单的类型,如 string、int 和 bool 作为属性。 They are in a different namespace than the SourceGenerator.它们位于与 SourceGenerator 不同的命名空间中。
There's a second console app project that references my source generator as an Analyzer.还有第二个控制台应用程序项目将我的源代码生成器作为分析器引用。 When I build the project I can succesfully break into the SourceGenerator code.当我构建项目时,我可以成功地进入 SourceGenerator 代码。
The problem: the simple ResponseMessage classes never get seen by the SyntaxReceiver class I have.问题:我拥有的 SyntaxReceiver 类永远不会看到简单的 ResponseMessage 类。 The OnVisitSyntaxNode
method only hits the Program.cs file. OnVisitSyntaxNode
方法仅命中 Program.cs 文件。 What is going on?到底是怎么回事?
By design, it's not possible to use a generator in the assembly where it was defined.根据设计,不可能在定义它的程序集中使用生成器。 According to the design spec :根据设计规范:
Generator implementations are defined in external assemblies passed to the compiler using the same
-analyzer:
option used for diagnostic analyzers.生成器实现在传递给编译器的外部程序-analyzer:
使用与诊断分析器相同的-analyzer:
选项定义。... ...
Since generators are loaded from external assemblies, a generator cannot be used to build the assembly in which it is defined.由于生成器是从外部程序集加载的,因此不能使用生成器来构建定义它的程序集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.