Using a Source Generator, I want to generate a single file that is built using an aggregate list of all the transformed nodes (instead of one at a time).
I’ll use an Enum-based example as these are common. Given (simplified for clarity):
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<MyEnumData?> sources = context.SyntaxProvider
.ForAttributeWithMetadataName(
MyEnumAttributeFullName,
predicate: (node, _) => node is EnumDeclarationSyntax,
transform: MyTransform)
.WithTrackingName("Gathering")
.Where(static node => node is not null)
.WithTrackingName("RemovingNulls");
context.RegisterSourceOutput(sources, static (context, sources) => Execute(context, in sources));
}
In this example, Execute is used to generate a source file based on the transformed MyEnumData for every enum tagged with MyEnumAttributeFullName.
My question: How do I generate/register a source file to be created bases on ALL of the generated MyEnumData (instead of one at a time)? I’m looking for an efficient compile-time solution that does not involve runtime reflection.
Basically I’m looking for the equivalent of:
public void RegisterSourceOutput<TSource>(IncrementalValuesProvider<TSource> source, Action<SourceProductionContext, ARRAY_OR_LIST_OF_ALL_TSOURCES> action)
Used to create to generate a single file that is built using an aggregated list of all the transformed nodes.
This could be used compose compile-time registries that avoid runtime reflection. In this enums example, this might be used to create a registry of all enums tagged with MyEnumAttributeFullName.
Lance Grooms is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.