Source Generator 在同一项目中未看到 类
Source Generator not seeing classes in same project
我有一个源代码生成器,它看起来像这样:
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);
}
}
}
在同一个项目中,我有一个简单的 classes 文件夹,其名称都以“ResponseMessage”结尾。这些 classes 只有简单的类型,如 string、int 和 bool 作为属性。它们位于与 SourceGenerator 不同的名称空间中。
有第二个控制台应用程序项目引用我的源代码生成器作为分析器。当我构建项目时,我可以成功地进入 SourceGenerator 代码。
问题:我拥有的 SyntaxReceiver class 从未看到简单的 ResponseMessage classes。 OnVisitSyntaxNode
方法只命中 Program.cs 文件。怎么回事?
根据设计,不可能在定义它的程序集中使用源代码生成器。根据the design spec:
Generator implementations are defined in external assemblies passed to the compiler using the same -analyzer:
option used for diagnostic analyzers.
...
Since generators are loaded from external assemblies, a generator cannot be used to build the assembly in which it is defined.
我有一个源代码生成器,它看起来像这样:
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);
}
}
}
在同一个项目中,我有一个简单的 classes 文件夹,其名称都以“ResponseMessage”结尾。这些 classes 只有简单的类型,如 string、int 和 bool 作为属性。它们位于与 SourceGenerator 不同的名称空间中。
有第二个控制台应用程序项目引用我的源代码生成器作为分析器。当我构建项目时,我可以成功地进入 SourceGenerator 代码。
问题:我拥有的 SyntaxReceiver class 从未看到简单的 ResponseMessage classes。 OnVisitSyntaxNode
方法只命中 Program.cs 文件。怎么回事?
根据设计,不可能在定义它的程序集中使用源代码生成器。根据the design spec:
Generator implementations are defined in external assemblies passed to the compiler using the same
-analyzer:
option used for diagnostic analyzers....
Since generators are loaded from external assemblies, a generator cannot be used to build the assembly in which it is defined.