CsvSerializerContext Class

The base class of the serializer contexts generated by the source generator, which provide the metadata of types without reflection.

public abstract class CsvSerializerContext : ICsvTypeInfoResolver

Remarks

Declare a partial class that derives from this class, with a CsvSerializableAttribute for each type to serialize. The generator adds a Default instance and a property per type that returns its CsvTypeInfo<T>, to pass to the methods of CsvSerializer. This works with trimming and Native AOT.

Example

[CsvSerializable(typeof(Product))]
[CsvSerializable(typeof(Person))]
[CsvSourceGenerationOptions(Separator = ';')]
public partial class AppCsvContext : CsvSerializerContext
{
}
Implements

Constructors

NameDescription
CsvSerializerContext(CsvSerializerOptions?)Initializes a new instance of the CsvSerializerContext class.

CsvSerializerContext(CsvSerializerOptions?)

CsvSerializerContext.cs

Initializes a new instance of the CsvSerializerContext class.

protected CsvSerializerContext(CsvSerializerOptions? options)

Parameters

options CsvSerializerOptions?
The options of the context, or null for the default options. The context uses a copy whose CsvSerializerOptions.TypeInfoResolver is the context.

Properties

NameDescription
OptionsGets the options of the context. Their CsvSerializerOptions.TypeInfoResolver is the context.

Gets the options of the context. Their CsvSerializerOptions.TypeInfoResolver is the context.

public CsvSerializerOptions Options { get; }

Property value

CsvSerializerOptions

Methods

NameDescription
GetOrCreateTypeInfo<T>()Gets the metadata of a type for CsvSerializerContext.Options, creating it on first use.
GetTypeInfo(Type, CsvSerializerOptions)Gets the serialization metadata of a type.

GetOrCreateTypeInfo<T>()

CsvSerializerContext.cs

Gets the metadata of a type for CsvSerializerContext.Options, creating it on first use.

protected CsvTypeInfo<T> GetOrCreateTypeInfo<T>()

Type parameters

T
The type.

Returns

CsvTypeInfo<T> The metadata of the type.

GetTypeInfo(Type, CsvSerializerOptions)

Gets the serialization metadata of a type.

public abstract CsvTypeInfo? GetTypeInfo(Type type, CsvSerializerOptions options)

Parameters

type Type
The type.
options CsvSerializerOptions
The options the metadata is created for.

Returns

CsvTypeInfo? The metadata, or null when the resolver does not support type.