Contexts
Pages 27
Clone this wiki locally
Contexts
Introduction
In addition to Options and IBoundConfiguration for controlling "high level" behavior during reading and writing, Cesil provides read and write specific contextual information at certain points during operation. Cesil also allows an arbitrary object
to be attached at these points.
ReadContext
ReadContext
is a readonly struct
which exposes the following members:
-
Options
- the current Options. -
Mode
- aReadContextMode
, one of:-
ReadContextMode.ReadingColumn
- when a particular column is being read. -
ReadContextMode.ReadingRow
- when a row is being read, but a particular column hasn't been decided on yet. -
ReadContextMode.ConvertingColumn
- when a particular column is being converted to a concrete type during dynamic deserialization. -
ReadContextMode.ConvertingRow
- when a particular row is being converted to a concrete type during dynamic deserialization.
-
-
RowNumber
- the 0-based index of the current row being read. -
HasColumn
-true
ifColumn
is set,false
ifColumn
will throw- This is a convenience property for checking if
Mode
is one ofReadContextMode.ReadingColumn
orReadContextMode.ConvertingColumn
- This is a convenience property for checking if
-
Column
- returns theColumnIdentifier
for the current column, if available. Throws if not available.-
Column
will only be available ifMode
is one ofReadContextMode.ReadingColumn
orReadContextMode.ConvertingColumn
-
-
Context
- anobject?
provided when theIReader<TRow>
orIAsyncReader<TRow>
was created
ReadContexts
are made available to all:
WriteContext
WriteContext
is a readonly struct
which exposes the following members:
-
Options
- the current Options. -
Mode
- aWriteContextMode
, one of:-
WriteContextMode.DiscoveringColumns
- when a dynamic writer is determining the columns in row. -
WriteContextMode.DiscoveringCells
- when a dynamic writer discovering cells in a row. -
WriteContextMode.WritingColumn
- when a writer of any kind is writing a single column.
-
-
HasRowNumber
-true
ifRowNumber
is set,false
ifRowNumber
will throw.- This is a convenience property for checking if
Mode
is one ofWriteContextMode.WritingColumn
orWriteContextMode.DiscoveringCells
.
- This is a convenience property for checking if
-
RowNumber
- returns the 0-based index of the row being written, if available. Throws if not available.-
RowNumber
will only be available ifMode
is one ofWriteContextMode.WritingColumn
orWriteContextMode.DiscoveringCells
.
-
-
Context
- anobject?
provided when theIWriter<TRow>
orIAsyncWriter<TRow>
was created.
WriteContexts
are made available to all:
Custom Contexts
As a convenience, Cesil allows an arbitrary object
to be attached to all ReadContexts
and WriteContexts
. To do so, use the context
parameter on any CreateXXX(...)
method on IBoundConfiguration<TRow>
.
Cesil treats any custom context as an opaque value, no attempt is made to synchronize access or manage lifetimes. If your context object requires special care when used, you will need to ensure proper care manually - this may require a custom ITypeDescriber
or a wrapper around the context object.