YamlSerializerContext Class

Base type for source-generated YAML serializer contexts.

public abstract class YamlSerializerContext : IYamlTypeInfoResolver
Implements

Constructors

NameDescription
YamlSerializerContext()Initializes a new instance of the YamlSerializerContext class.
YamlSerializerContext(YamlSerializerOptions)Initializes a new instance of the YamlSerializerContext class.

YamlSerializerContext overloads

YamlSerializerContext()

YamlSerializerContext.cs

Initializes a new instance of the YamlSerializerContext class.

protected YamlSerializerContext()

YamlSerializerContext(YamlSerializerOptions)

YamlSerializerContext.cs

Initializes a new instance of the YamlSerializerContext class.

protected YamlSerializerContext(YamlSerializerOptions options)
Parameters
options YamlSerializerOptions
The options used by this context.
Exceptions
ArgumentNullException
options is null.
ArgumentException
options specifies a YamlSerializerOptions.TypeInfoResolver that is not this context.

Properties

NameDescription
OptionsGets the options instance associated with this context.

Gets the options instance associated with this context.

public YamlSerializerOptions Options { get; }

Property value

YamlSerializerOptions

Remarks

This is the options instance used by generated metadata properties on the context.

Methods

NameDescription
CreateOptions(Func<YamlSerializerOptions, YamlSerializerOptions>)Creates a new YamlSerializerOptions based on this context's options with the specified overrides applied, while preserving the YamlSerializerOptions.TypeInfoResolver.
GetTypeInfo(Type, YamlSerializerOptions)Gets type metadata for a CLR type.

CreateOptions(Func<YamlSerializerOptions, YamlSerializerOptions>)

YamlSerializerContext.cs

Creates a new YamlSerializerOptions based on this context's options with the specified overrides applied, while preserving the YamlSerializerOptions.TypeInfoResolver.

Parameters

configure Func<YamlSerializerOptions, YamlSerializerOptions>
A function that applies overrides to a copy of this context's options using the with expression.

Returns

YamlSerializerOptions A new options instance with the configured overrides and this context as the resolver.

Exceptions

ArgumentNullException
configure is null.

Remarks

This is useful when you need per-call option variations (such as a different YamlSerializerOptions.SourceName for each file) while reusing the same source-generated context.

If the overrides include runtime YamlSerializerOptions.Converters, generated contexts resolve supported converter replacements when a generated YamlTypeInfo is initialized. Reuse that YamlTypeInfo for repeated operations with the same converter set to avoid reinitializing option-specific generated metadata.

The returned options always has YamlSerializerOptions.TypeInfoResolver set to this context. Any YamlSerializerOptions.TypeInfoResolver value set by configure is overwritten.

Example

var context = new TestYamlSerializerContext();
var options = context.CreateOptions(o => o with { SourceName = "config.yaml" });
var person = YamlSerializer.Deserialize<GeneratedPerson>("first_name: John\n", options);
Assert.Equal("John", person!.FirstName);

GetTypeInfo(Type, YamlSerializerOptions)

Gets type metadata for a CLR type.

public abstract YamlTypeInfo? GetTypeInfo(Type type, YamlSerializerOptions options)

Parameters

type Type
The CLR type to resolve.
options YamlSerializerOptions
The serializer options in use.

Returns

YamlTypeInfo? The metadata for type, or null when not available.