YamlSerializerContext Class
Base type for source-generated YAML serializer contexts.
public abstract class YamlSerializerContext : IYamlTypeInfoResolver- Implements
Constructors
| Name | Description |
|---|---|
| YamlSerializerContext() | Initializes a new instance of the YamlSerializerContext class. |
| YamlSerializerContext(YamlSerializerOptions) | Initializes a new instance of the YamlSerializerContext class. |
YamlSerializerContext overloads
YamlSerializerContext()
YamlSerializerContext.csInitializes a new instance of the YamlSerializerContext class.
protected YamlSerializerContext()YamlSerializerContext(YamlSerializerOptions)
YamlSerializerContext.csInitializes a new instance of the YamlSerializerContext class.
protected YamlSerializerContext(YamlSerializerOptions options)Parameters
optionsYamlSerializerOptions- The options used by this context.
Exceptions
ArgumentNullExceptionoptionsisnull.ArgumentExceptionoptionsspecifies aYamlSerializerOptions.TypeInfoResolverthat is not this context.
Properties
| Name | Description |
|---|---|
| Options | Gets the options instance associated with this context. |
Options
YamlSerializerContext.csGets the options instance associated with this context.
public YamlSerializerOptions Options { get; }Property value
Remarks
This is the options instance used by generated metadata properties on the context.
Methods
| Name | Description |
|---|---|
| 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.csCreates a new YamlSerializerOptions based on this context's options with the specified overrides applied, while preserving the YamlSerializerOptions.TypeInfoResolver.
public YamlSerializerOptions CreateOptions(Func<YamlSerializerOptions, YamlSerializerOptions> configure)Parameters
configureFunc<YamlSerializerOptions, YamlSerializerOptions>- A function that applies overrides to a copy of this context's options using the
withexpression.
Returns
YamlSerializerOptions A new options instance with the configured overrides and this context as the resolver.
Exceptions
ArgumentNullExceptionconfigureisnull.
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
typeType- The CLR type to resolve.
optionsYamlSerializerOptions- The serializer options in use.
Returns
YamlTypeInfo? The metadata for type, or null when not available.