admin管理员组文章数量:1391999
I'm writing a source generator which will generate factories to create instances of object(s).
Since the source generator generates code that's related to tests, I'm going to install the source generator in the actual test project.
Now, assume that the source project contains a simple record, such as a Person
, I want the source generator to find this one, and then based on that one, generate some code.
Here's the source generator currently:
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValueProvider<ImmutableArray<TypeBuilderMetadata?>> provider = context
.SyntaxProvider.CreateSyntaxProvider(
(node, token) =>
{
token.ThrowIfCancellationRequested();
return node is RecordDeclarationSyntax;
},
(ctx, token) =>
{
token.ThrowIfCancellationRequested();
var symbol = (RecordDeclarationSyntax)ctx.Node;
if (ctx.SemanticModel.GetDeclaredSymbol(symbol, token) is not INamedTypeSymbol recordSymbol)
{
return null;
}
var fullTypeName = recordSymbol.ToDisplayString();
var typeNamespace = recordSymbol.GetFullNamespace();
return NewTypeBuilderDefinition(recordSymbol, recordSymbol);
}
)
.Where(builder => builder is not null)
.Collect();
context.RegisterSourceOutput(provider, this.GenerateSource);
}
Now, since the test project doesn't contain any 'Record' definitions, the source generator will never generate code. This is because the SyntaxProvider does look at the current project, and not at the entire compilation.
Is there a way to look at the syntax trees of the entire compilation?
本文标签: roslynC Source generator accessing code from quotReferenced Project(s)quotStack Overflow
版权声明:本文标题:roslyn - C# Source generator accessing code from "Referenced Project(s)" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744662521a2618331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论