Fork me on GitHub

Type Conversion

CsvHelper uses type converters to convert a string into an object, and an object into a string.

ITypeConverter


The interface that all type converters must implement.

public interface ITypeConverter
{
    string ConvertToString( object value, IWriterRow row, MemberMapData memberMapData );

    string ConvertFromString( string text, IReaderRow row, MemberMapData memberMapData );
}

ConvertToString

Takes an object and converts it to a string.

Parameters

  • value: The object to convert that can be a value or reference type.
  • row: The IWriterRow to allow the converter to write, have access to the configuration, and context.
  • memberMapData: The configured map data for the member that is being converted.

ConvertFromString

Takes a string and converts it to an object.

Parameters

  • text: The string to convert.
  • row: The IReaderRow to allow the converter to read, have access to the configuration, and context.
  • memberMapData: The configured map data for the member that is being converted.

DefaultTypeConverter


The default converter that can be used as a base for other converters.

ConvertToString will ToString on the object using any formatting options that were set.

ConvertFromString will throw a TypeConverterException. Classes that implement DefaultTypeConverter need to override this method and provide an implementation for it's type.

TypeConverterOptions


TypeConverterCache


TypeConverterOptionsCache


Type Converters


These are the available converters. For the most part, they just mimic what the .NET type converters do, except these take into account the configuration and mapping settings.

ArrayConverter

Converts multiple fields to and from an Array object member.

// Data
A,B,C,C,C

BooleanConverter

ByteArrayConverter

ByteArrayConverterOptions

ByteConverter

CharConverter

CollectionGenericConverter

DateTimeConverter

DateTimeOffsetConverter

DecimalConverter

DoubleConverter

EnumConverter

EnumerableConverter

GuidConverter

IDictionaryConverter

IDictionaryGenericConverter

IEnumerableConverter

IEnumerableGenericConverter

Int16Converter

Int32Converter

Int62Converter

NullableConverter

SByteConverter

SingleConverter

StringConverter

TimeSpanConverter

UInt16Converter

UInt32Converter

UInt64Converter