ContextExtensions
Extension methods for adding generated source code to a generator context.
internal static class ContextExtensionsExamples
Section titled “Examples”The following example demonstrates how to add generated source code to the compilation from an incremental generator.
using Hertzole.SourceGen;using Microsoft.CodeAnalysis;
public class MyGenerator : IIncrementalGenerator{ public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterSourceOutput(context.CompilationProvider, (productionContext, _) => { // Must dispose when the writer is no longer needed. using CodeWriter writer = new CodeWriter(); writer.AppendLine("namespace My.Namespace"); using (writer.WithBlock()) { writer.AppendLine("public class GeneratedClass { }"); }
// Adds the writer's contents to the compilation and clears the writer. productionContext.AddSource("My.Namespace.GeneratedClass.g.cs", writer); }); }}The following example adds source code using a specific encoding instead of the default UTF-8.
using System.Text;using Hertzole.SourceGen;using Microsoft.CodeAnalysis;
public static class SourceEmitter{ public static void Emit(SourceProductionContext context) { using CodeWriter writer = new CodeWriter(); writer.AppendLine("// Generated code.");
context.AddSource("Generated.g.cs", writer, Encoding.UTF8); }}Methods
Section titled “Methods”AddSource(GeneratorExecutionContext, string, CodeWriter)
Section titled “AddSource(GeneratorExecutionContext, string, CodeWriter)”Adds source code from the provided CodeWriter to the compilation with UTF-8 encoding.
public static void AddSource(this GeneratorExecutionContext context, string hintName, CodeWriter writer);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(GeneratorExecutionContext, string, CodeWriter, Encoding)
Section titled “AddSource(GeneratorExecutionContext, string, CodeWriter, Encoding)”Adds source code from the provided CodeWriter to the compilation with a specified Encoding.
public static void AddSource(this GeneratorExecutionContext context, string hintName, CodeWriter writer, Encoding encoding);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
encoding
Section titled “encoding”Encoding of the file that will be saved.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(GeneratorPostInitializationContext, string, CodeWriter)
Section titled “AddSource(GeneratorPostInitializationContext, string, CodeWriter)”Adds source code from the provided CodeWriter to the compilation with UTF-8 encoding.
public static void AddSource(this GeneratorPostInitializationContext context, string hintName, CodeWriter writer);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(GeneratorPostInitializationContext, string, CodeWriter, Encoding)
Section titled “AddSource(GeneratorPostInitializationContext, string, CodeWriter, Encoding)”Adds source code from the provided CodeWriter to the compilation with a specified Encoding.
public static void AddSource(this GeneratorPostInitializationContext context, string hintName, CodeWriter writer, Encoding encoding);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
encoding
Section titled “encoding”Encoding of the file that will be saved.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(IncrementalGeneratorPostInitializationContext, string, CodeWriter)
Section titled “AddSource(IncrementalGeneratorPostInitializationContext, string, CodeWriter)”Adds source code from the provided CodeWriter to the compilation with UTF-8 encoding.
public static void AddSource(this IncrementalGeneratorPostInitializationContext context, string hintName, CodeWriter writer);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(IncrementalGeneratorPostInitializationContext, string, CodeWriter, Encoding)
Section titled “AddSource(IncrementalGeneratorPostInitializationContext, string, CodeWriter, Encoding)”Adds source code from the provided CodeWriter to the compilation with a specified Encoding.
public static void AddSource(this IncrementalGeneratorPostInitializationContext context, string hintName, CodeWriter writer, Encoding encoding);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
encoding
Section titled “encoding”Encoding of the file that will be saved.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(SourceProductionContext, string, CodeWriter)
Section titled “AddSource(SourceProductionContext, string, CodeWriter)”Adds source code from the provided CodeWriter to the compilation with UTF-8 encoding.
public static void AddSource(this SourceProductionContext context, string hintName, CodeWriter writer);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.
AddSource(SourceProductionContext, string, CodeWriter, Encoding)
Section titled “AddSource(SourceProductionContext, string, CodeWriter, Encoding)”Adds source code from the provided CodeWriter to the compilation with a specified Encoding.
public static void AddSource(this SourceProductionContext context, string hintName, CodeWriter writer, Encoding encoding);context
Section titled “context”The context to add the source to.
hintName
Section titled “hintName”An identifier that can be used to reference this source text, must be unique within this generator.
writer
Section titled “writer”The writer containing the source code to add.
encoding
Section titled “encoding”Encoding of the file that will be saved.
Remarks
Section titled “Remarks”The writer will be cleared after adding the source.