The boolean values overload for TypeConverterOption
is used only when reading. It allows you to specify multiple values that can be used for true/false when reading. So you could do 1
, "true"
, "TRUE"
, "True"
, "yes"
, etc.
Currently the only way to do it when writing is to create a custom type converter.
public class MyBooleanConverter : DefaultTypeConverter
{
public override string ConvertToString( TypeConverterOptions options, object value )
{
if( value == null )
{
return string.Empty;
}
var boolValue = (bool)value;
return boolValue ? "yes" : "no";
}
}
You can then apply it to all booleans globally.
CsvHelper.TypeConversion.TypeConverterFactory.AddConverter<bool>( new MyBooleanConverter() );
Or apply it to a single property via the mapping.
Map( m => m.MyBoolProperty ).TypeConverter<MyBooleanConverter>();