TomlConverterFactory Class
Definition
- Namespace
- Bodu.Text.Toml.Serialization
- Assembly
- Bodu.Text.Toml.dll
- Package
- Bodu.Text.Toml 1.0.0
- Source
- ConverterFactory.cs
Produces a TomlConverter for a family of types that cannot be served by a single TomlConverter<T> - for example every Nullable<T>, every enumeration, or every collection.
public abstract class TomlConverterFactory : TomlConverter
- Inheritance
-
TomlConverterFactory
- Derived
- Inherited Members
- Extension Methods
Examples
public sealed class StackConverterFactory : TomlConverterFactory
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(Stack<>);
public override TomlConverter CreateConverter(Type typeToConvert, TomlSerializerOptions options) =>
(TomlConverter)Activator.CreateInstance(
typeof(StackConverter<>).MakeGenericType(typeToConvert.GetGenericArguments()[0]))!;
}
Remarks
The serializer calls CanConvert(Type) to decide whether the factory applies, then CreateConverter(Type, TomlSerializerOptions) to obtain the converter for the specific closed type being serialized. A factory is never asked to read or write a value itself.
Constructors
TomlConverterFactory()
Initializes a new instance of the TomlConverterFactory class.
protected TomlConverterFactory()
Methods
CreateConverter(Type, TomlSerializerOptions)
Creates a converter for the specified type.
public abstract TomlConverter CreateConverter(Type typeToConvert, TomlSerializerOptions options)
Parameters
typeToConvertTypeThe closed type to create a converter for.
optionsTomlSerializerOptionsThe serializer options in effect.
Returns
- TomlConverter
A converter that handles
typeToConvert.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |