Skip to content

Support Types

Additional types supporting the emit API.

See also: Emit Overview


AttributeBuilder

Create attribute declarations.

Factory Methods

Method Description
For(string name) Create an attribute

Arguments

attr.WithArgument("\"value\"")
attr.WithArguments("1", "2", "3")
attr.WithNamedArgument("Name", "\"value\"")

Usings

attr.AddUsing("System.ComponentModel")

Properties

attr.Name    // string
attr.Usings  // ImmutableArray<string>

XmlDocumentationBuilder

Create XML documentation comments.

Factory Methods

Method Description
Create() Create empty documentation
WithSummary(string) Create with just a summary
From(XmlDocumentation) Create from existing parsed documentation

Content

doc.Summary("Gets the customer name.")
doc.Remarks("This method queries the database.")
doc.Returns("The customer name if found; otherwise, null.")
doc.Value("The property value.")
doc.Param("id", "The customer identifier.")
doc.TypeParam("T", "The entity type.")
doc.Exception("InvalidOperationException", "Thrown when...")
doc.SeeAlso("OtherClass")
doc.Example("<code>var name = GetName(123);</code>")

Raw XML Mode

When content contains intentional XML markup (e.g., <see cref="..."/>, <list> elements), enable raw XML mode to skip escaping:

doc.WithRawXml()

This affects content in params, returns, value, exceptions, and seealso — summary and remarks are always unescaped.

Properties

doc.HasContent  // bool
doc.RawXml      // bool

EmitOptions

Configure code emission. Immutable after construction — use object initializer syntax.

Properties

Property Type Default Description
ValidationLevel ValidationLevel Syntax Validation level (None, Syntax, Semantic, Full)
Indentation string " " Indentation string (4 spaces)
EndOfLine string "\n" Line ending (Unix-style)
HeaderComment string "// <auto-generated/>" Comment at top of file
LicenseHeader string? null License header text — set explicitly for your project
UseRegions bool false Wrap member categories in #region blocks

Static Factories

EmitOptions.Default      // syntax validation, standard formatting, no license header
EmitOptions.NoValidation // skip validation (fastest)

Usage

// Custom options with license header
var options = new EmitOptions
{
    LicenseHeader = "// SPDX-License-Identifier: MIT",
    UseRegions = true
};

var result = builder.Emit(options);

No default license header

LicenseHeader defaults to null. If your generated files need a license header, set it explicitly in your generator's emit options.

ValidationLevel Enum

Value Description
None No validation — fastest
Syntax Syntax validation only (default) — parses generated C# for syntax errors
Semantic Semantic validation — validates type references (reserved, not yet implemented)
Full Full validation — all checks including naming conventions (reserved, not yet implemented)

Directive

Represents a preprocessor directive condition. See Directives for full documentation.

// Pre-defined directives
Directives.Net6OrGreater
Directives.Net7OrGreater

// Custom directive
Directives.Custom("MY_FEATURE_FLAG")

// Combining directives
Directives.Net6OrGreater.And(Directives.Custom("ENABLE_SPANS"))
Directives.Net6OrGreater.Or(Directives.NetStandard21OrGreater)
Directives.NetFramework.Not()

ConditionalInterface

Represents an interface implementation that may be conditionally compiled.

// Implicit conversion from string (unconditional)
ConditionalInterface iface = "IEquatable<UserId>";

// Explicit conditional interface
var conditional = new ConditionalInterface("ISpanFormattable", Directives.Net6OrGreater);

// Used with TypeBuilder.Implements
builder.Implements("ISpanFormattable", Directives.Net6OrGreater);

IWritable

Contract for emit components that can write their C# representation to an EmitWriter.

public interface IWritable
{
    void WriteTo(EmitWriter writer);
}

Implement this interface to participate in the string-based emission pipeline:

public class MyComponent : IWritable
{
    public void WriteTo(EmitWriter writer)
    {
        writer.WriteLine("// generated by MyComponent");
    }
}

// Use with EmitWriter
writer.Write(myComponent); // delegates to WriteTo