Class CsvAsyncWriter<T>
- Namespace
- FlameCsv
- Assembly
- FlameCsv.Core.dll
Instance that provides convenience methods around CsvFieldWriter<T>.
public class CsvAsyncWriter<T> : IAsyncDisposable where T : unmanaged, IBinaryInteger<T>
Type Parameters
T
Token type
- Inheritance
-
CsvAsyncWriter<T>
- Implements
- Derived
Constructors
CsvAsyncWriter(CsvFieldWriter<T>, bool)
Initializes a new writer instance.
public CsvAsyncWriter(CsvFieldWriter<T> inner, bool autoFlush)
Parameters
inner
CsvFieldWriter<T>Field writer instance to write to
autoFlush
boolWhether to automatically flush after each record if the writer's buffer pressure is high enough. Automatic flushing is performed in NextRecord() and NextRecordAsync(CancellationToken).
Properties
AutoFlush
Whether to automatically check if the writer needs to be flushed after each record.
public bool AutoFlush { get; set; }
Property Value
- See Also
ColumnIndex
0-based index of the current column/field. Reset after each newline.
public int ColumnIndex { get; protected set; }
Property Value
ExpectedFieldCount
Field count required for each record, if set.
public int? ExpectedFieldCount { get; set; }
Property Value
- int?
Remarks
Set automatically after the first non-empty record if ValidateFieldCount is true.
Inner
Inner field writer instance.
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected CsvFieldWriter<T> Inner { get; }
Property Value
IsCompleted
Whether the writer has completed (disposed).
public bool IsCompleted { get; }
Property Value
LineIndex
1-based index of the current line/record. Incremented after each newline.
public int LineIndex { get; protected set; }
Property Value
Remarks
Newlines in quoted fields/strings are not counted, this property represents the logical CSV record index.
Options
Options instance of this writer.
public CsvOptions<T> Options { get; }
Property Value
- CsvOptions<T>
Methods
CompleteAsync(Exception?, CancellationToken)
Completes the writer, flushing any remaining data if exception
is null.
Multiple completions are no-ops.
public ValueTask CompleteAsync(Exception? exception = null, CancellationToken cancellationToken = default)
Parameters
exception
ExceptionObserved exception when writing the data, passed to the inner ICsvPipeWriter<T>. If not null, the final buffer is not flushed and the exception is rethrown.
cancellationToken
CancellationTokenToken to cancel the operation
Returns
FlushAsync(CancellationToken)
Flushes the writer.
public ValueTask FlushAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenToken to cancel flushing
Returns
Exceptions
- ObjectDisposedException
Thrown if the writer has completed (see CompleteAsync(Exception?, CancellationToken)).
GetDematerializerAndIncrementFieldCount<TRecord>()
Returns or creates a cached dematerializer using TypeBinder, and pre-increments ColumnIndex by FieldCount.
[RequiresUnreferencedCode("This code path uses reflection. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
[RequiresDynamicCode("This code path uses compiled expressions. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
protected IDematerializer<T, TRecord> GetDematerializerAndIncrementFieldCount<TRecord>()
Returns
- IDematerializer<T, TRecord>
Type Parameters
TRecord
Type to write
GetDematerializerAndIncrementFieldCount<TRecord>(CsvTypeMap<T, TRecord>)
Returns or creates a cached dematerializer using the type map, and pre-increments ColumnIndex by FieldCount.
protected IDematerializer<T, TRecord> GetDematerializerAndIncrementFieldCount<TRecord>(CsvTypeMap<T, TRecord> typeMap)
Parameters
typeMap
CsvTypeMap<T, TRecord>Type map instance
Returns
- IDematerializer<T, TRecord>
Type Parameters
TRecord
Type to write
NextRecordAsync(CancellationToken)
Writes a newline and flushes the buffer if needed when AutoFlush is true.
public ValueTask NextRecordAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationTokenToken to cancel the flush
Returns
Exceptions
- ObjectDisposedException
The writer has completed
ValidateFieldCount()
Validates the current record's field count if ValidateFieldCount is true. Called when moving to the next record.
protected void ValidateFieldCount()
WriteDelimiterIfNeeded()
Writes a delimiter if the current column index is not 0.
protected void WriteDelimiterIfNeeded()
WriteField(ReadOnlySpan<char>, bool)
Writes a field with the preceding delimiter if needed.
public void WriteField(ReadOnlySpan<char> text, bool skipEscaping = false)
Parameters
text
ReadOnlySpan<char>Value to write
skipEscaping
boolWhether no escaping should be performed, use with care
WriteField(ReadOnlySpan<T>, bool)
Writes a field with the preceding delimiter if needed.
public void WriteField(ReadOnlySpan<T> text, bool skipEscaping = false)
Parameters
text
ReadOnlySpan<T>Value to write
skipEscaping
boolWhether no escaping should be performed, use with care
WriteField<TField>(CsvConverter<T, TField>, TField?)
Writes a field with the preceding delimiter if needed.
public void WriteField<TField>(CsvConverter<T, TField> converter, TField? value)
Parameters
converter
CsvConverter<T, TField>Converter instance to write the value with
value
TFieldValue to write
Type Parameters
TField
Field type that will be converted
WriteField<TField>(TField?)
Writes a field with the preceding delimiter if needed.
[RequiresDynamicCode("This code path may require types that cannot be statically analyzed and might need runtime code generation. Use an alternative overload for native AOT applications.")]
[RequiresUnreferencedCode("This code path may require types that cannot be statically analyzed and might need runtime code generation. Use an alternative overload for native AOT applications.")]
public void WriteField<TField>(TField? value)
Parameters
value
TFieldValue to write
Type Parameters
TField
Field type that will be converted
Remarks
Converter will be retrieved from options.
WriteHeader<TRecord>()
Writes the header for TRecord
to the current line using TypeBinder.
[RequiresUnreferencedCode("This code path uses reflection. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
[RequiresDynamicCode("This code path uses compiled expressions. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
public void WriteHeader<TRecord>()
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteHeader<TRecord>(CsvTypeMap<T, TRecord>)
Writes the header for TRecord
to the current line using the type map.
public void WriteHeader<TRecord>(CsvTypeMap<T, TRecord> typeMap)
Parameters
typeMap
CsvTypeMap<T, TRecord>Type map to use for writing
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteRaw(ReadOnlySpan<T>, int, int)
Writes a sequence of raw characters to the writer. ColumnIndex and LineIndex are not tracked automatically, and no escaping is performed.
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void WriteRaw(ReadOnlySpan<T> value, int columnsWritten = 0, int linesWritten = 0)
Parameters
value
ReadOnlySpan<T>Value to write
columnsWritten
intHow many columns the value spans
linesWritten
intHow many new lines the value spans
WriteRecord<TRecord>(CsvTypeMap<T, TRecord>, TRecord)
Writes the value to the current line using the type map.
public void WriteRecord<TRecord>(CsvTypeMap<T, TRecord> typeMap, TRecord value)
Parameters
typeMap
CsvTypeMap<T, TRecord>Type map to use for writing
value
TRecordValue to write
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).
WriteRecord<TRecord>(TRecord)
Writes the value to the current line using TypeBinder.
[RequiresUnreferencedCode("This code path uses reflection. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
[RequiresDynamicCode("This code path uses compiled expressions. Use the overloads accepting source generated CsvTypeMap for AOT/trimming compatible code.")]
public void WriteRecord<TRecord>(TRecord value)
Parameters
value
TRecordValue to write
Type Parameters
TRecord
Remarks
Does not write a trailing newline, see NextRecord() and NextRecordAsync(CancellationToken).