This Generator is intendede to generate text that a source generator can use to emit source to its generation.
E.g. Instead of writing a String that contains the definiton of an Attribute (without syntax highlighting and checking).
You can generate the attribute normaly in Code and anotate it wit [SourceGenerator.Helper.CopyCode.Copy].
Attributes defined on that Type will also be copied, if they are defined below the [SourceGenerator.Helper.CopyCode.Copy]-Attribute.
Assume you have the following attribute:
namespaceSourceGenerator.Helper.CopyCode.Example;[SourceGenerator.Helper.CopyCode.Copy][System.AttributeUsage(AttributeTargets.All,Inherited=false,AllowMultiple=false)]internalsealedclassMyGeneratorAttribute:Attribute{}then the generator will generate:
// <auto-generated/>
#nullable enable
namespaceSourceGenerator.Helper.CopyCode;internalstaticpartialclassCopy{publicconststringSourceGeneratorHelperCopyCodeExampleMyGeneratorAttribute=""" // <auto-generated/> #nullable enable namespace SourceGenerator.Helper.CopyCode.Example; [System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] internal sealed class MyGeneratorAttribute : Attribute { } """;}And your Generator can emit it:
[Generator(LanguageNames.CSharp)]publicclassMyGenerator:IIncrementalGenerator{publicvoidInitialize(IncrementalGeneratorInitializationContextcontext){context.RegisterPostInitializationOutput(context =>context.AddSource("attribute.g.cs",SourceGenerator.Helper.CopyCode.Copy.SourceGeneratorHelperCopyCodeExampleMyGeneratorAttribute));// The rest of your generator…}}