Skip to content

ContextExtensions

Extension methods for adding generated source code to a generator context.

C# Signature
internal static class ContextExtensions

The following example demonstrates how to add generated source code to the compilation from an incremental generator.

C#
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.

C#
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);
}
}

AddSource(GeneratorExecutionContext, string, CodeWriter)

Section titled “AddSource(GeneratorExecutionContext, string, CodeWriter)”

Adds source code from the provided CodeWriter to the compilation with UTF-8 encoding.

C# Signature
public static void AddSource(this GeneratorExecutionContext context, string hintName, CodeWriter writer);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

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.

C# Signature
public static void AddSource(this GeneratorExecutionContext context, string hintName, CodeWriter writer, Encoding encoding);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

Encoding of the file that will be saved.

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.

C# Signature
public static void AddSource(this GeneratorPostInitializationContext context, string hintName, CodeWriter writer);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

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.

C# Signature
public static void AddSource(this GeneratorPostInitializationContext context, string hintName, CodeWriter writer, Encoding encoding);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

Encoding of the file that will be saved.

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.

C# Signature
public static void AddSource(this IncrementalGeneratorPostInitializationContext context, string hintName, CodeWriter writer);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

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.

C# Signature
public static void AddSource(this IncrementalGeneratorPostInitializationContext context, string hintName, CodeWriter writer, Encoding encoding);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

Encoding of the file that will be saved.

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.

C# Signature
public static void AddSource(this SourceProductionContext context, string hintName, CodeWriter writer);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

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.

C# Signature
public static void AddSource(this SourceProductionContext context, string hintName, CodeWriter writer, Encoding encoding);

The context to add the source to.

An identifier that can be used to reference this source text, must be unique within this generator.

The writer containing the source code to add.

Encoding of the file that will be saved.

The writer will be cleared after adding the source.