I have following Execute method in my source generator. I can get all the properties (x.Item1.Members.AsEnumerable()...) of my class. But I can't manage to get the properties of the base class too. How can I list all properties of the base class?
private static void Execute(Compilation compilation, ImmutableArray<(ClassDeclarationSyntax, AttributeData)> classes, SourceProductionContext context)
{
foreach (var (x, i) in classes.Select((x, i) => (x, i)))
{
TypedConstant aggregateParam = x.Item2.ConstructorArguments[0];
if (aggregateParam.Kind == TypedConstantKind.Primitive &&
aggregateParam.Value is string fullyQualifiedAggregateName)
{
var aggregateName = "Aggregate";
var props = x.Item1.Members.AsEnumerable().Where(o => o.IsKind(SyntaxKind.PropertyDeclaration));
var sb = new StringBuilder();
context.AddSource(
$"generated_{aggregateName}_{i}.g.cs",
sb.ToString());
}
}
}
Update
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<(ClassDeclarationSyntax, AttributeData)> classDeclarations = context.SyntaxProvider
.ForAttributeWithMetadataName(
EventApplyAttribute,
predicate: static (node, _) => node is ClassDeclarationSyntax,
transform: static (ctx, ct) => GetSemanticTargetForGeneration(ctx, ct))
.Where(m => m.Item1 is not null && m.Item2 is not null);
IncrementalValueProvider<(Compilation, ImmutableArray<(ClassDeclarationSyntax, AttributeData)>)> compilationAndClasses
= context.CompilationProvider.Combine(classDeclarations.Collect());
context.RegisterSourceOutput(compilationAndClasses,
static (spc, source) => Execute(source.Item1, source.Item2, spc));
}
private static (ClassDeclarationSyntax, AttributeData) GetSemanticTargetForGeneration(GeneratorAttributeSyntaxContext context, CancellationToken ct)
{
if (context.TargetNode is not ClassDeclarationSyntax classDeclaration)
{
return (null, null);
}
AttributeData? attribute = context.Attributes.FirstOrDefault(a => a.AttributeClass?.Name == "EventApplyAttribute");
return (classDeclaration, attribute);
}
RegisterImplementationSourceOutputin the incremental pipeline, it literally gives you theCompilationin the callback, which is presumably what Dani is talking about here, since that's also the API that gives youImmutableArray<TSource>for yourTSource(in this caseClassDeclarationSyntax); it is literally impossible to avoid theCompilation