Table of Contents

YamlConverterFactory Class

Definition

Namespace
Bodu.Text.Yaml.Serialization
Assembly
Bodu.Text.Yaml.dll
Package
Bodu.Text.Yaml 1.0.0
Source
ConverterFactory.cs

Produces a YamlConverter for a family of types that cannot be served by a single YamlConverter<T> - for example every Nullable<T>, every enumeration, or every collection.

public abstract class YamlConverterFactory : YamlConverter
Inheritance
YamlConverterFactory
Derived
Inherited Members
Extension Methods

Examples

public sealed class StackConverterFactory : YamlConverterFactory
{
    public override bool CanConvert(Type typeToConvert) =>
        typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(Stack<>);

    public override YamlConverter CreateConverter(Type typeToConvert, YamlSerializerOptions options) =>
        (YamlConverter)Activator.CreateInstance(
            typeof(StackConverter<>).MakeGenericType(typeToConvert.GetGenericArguments()[0]))!;
}

Remarks

The serializer calls CanConvert(Type) to decide whether the factory applies, then CreateConverter(Type, YamlSerializerOptions) to obtain the converter for the specific closed type being serialized. A factory is never asked to read or write a value itself.

Constructors

YamlConverterFactory()

Initializes a new instance of the YamlConverterFactory class.

protected YamlConverterFactory()

Methods

CreateConverter(Type, YamlSerializerOptions)

Creates a converter for the specified type.

public abstract YamlConverter CreateConverter(Type typeToConvert, YamlSerializerOptions options)

Parameters

typeToConvert Type

The closed type to create a converter for.

options YamlSerializerOptions

The serializer options in effect.

Returns

YamlConverter

A converter that handles typeToConvert.

Applies to

ProductVersions
.NET8, 10