Table of Contents

BencodeConverterFactory Class

Definition

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

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

public abstract class BencodeConverterFactory : BencodeConverter
Inheritance
BencodeConverterFactory
Derived
Inherited Members
Extension Methods

Examples

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

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

Remarks

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

Constructors

BencodeConverterFactory()

Initializes a new instance of the BencodeConverterFactory class.

protected BencodeConverterFactory()

Methods

CreateConverter(Type, BencodeSerializerOptions)

Creates a converter for the specified type.

public abstract BencodeConverter CreateConverter(Type typeToConvert, BencodeSerializerOptions options)

Parameters

typeToConvert Type

The closed type to create a converter for.

options BencodeSerializerOptions

The serializer options in effect.

Returns

BencodeConverter

A converter that handles typeToConvert.

Applies to

ProductVersions
.NET8, 10