Using specific instances of TypeConverters - thiscode/CsvHelper GitHub Wiki

To use this library in a project with many files from many countries and many different companies you must write in the most cases a set of TypeConverter. The most of them differ only by the usage of NumberStyles. Sometimes in one file are different formats. The other thing you have everytime to do is to set the CurrentCulture of the Thread for every csv file according to the country it comes from. Here are the modifications to handle such projects easier:

Create Converter on the Fly

The best way to handle such a project is to use fluent mapping and map every properties with a special converter. To not always write the same code, the most default converter has now additional constructors:

Decimal-, Double-, Single-Converter:

Converter(NumberStyles UseNumberStyles)
Converter(CultureInfo UseCultureInfo)
Converter(CultureInfo UseCultureInfo, NumberStyles UseNumberStyles)

Use the specified NumberStyles and/or Culture. The default constructor use NumberStyles.Float and the culture specified in configuration.

Byte-, Int16-, Int32-, Int64-, SByte-, UInt16-, UInt32- and UInt64-Converter:

Converter(NumberStyles UseNumberStyles)
Converter(CultureInfo UseCultureInfo)
Converter(CultureInfo UseCultureInfo, NumberStyles UseNumberStyles)

Use the specified NumberStyles and/or Culture. The default constructor use NumberStyles.Integer and the culture specified in configuration.

Boolean-Converter:

BooleanConverter(List<string> trueStrings, List<string> falseStrings)

You can specify a list of representations for "true" and for "false". The default constructor use "y", "yes" and "n", "no".

DateTime-Converter:

DateTimeConverter(CultureInfo UseCultureInfo)
DateTimeConverter(DateTimeStyles UseDateTimeStyles)
DateTimeConverter(CultureInfo UseCultureInfo, DateTimeStyles UseDateTimeStyles)

Use the CultureInfo and DateTimeStyles to convert the string into a datetime-value. See "DateTime.TryParse" for more details.

How To Use (fluent):

You can instantiate a converter and use this instance directly for converting a property. The first way already exists in CsvHelper, but it is undocumented. Use the instance with fluent mapping:

Map(m => m.MyProperty).TypeConverter(InstanceOfMyCustomTypeConverter);

Here a full example:

    public sealed class CustomClassMap : CsvClassMap<ItemSpec>
    {
        public CustomClassMap()
        {

			//In the most casess you will need one instance more then one time.
            ITypeConverter IntAllowThousands = new Int32Converter(NumberStyles.Integer | NumberStyles.AllowThousands);

			//You can use instances you already have.
            Map(m => m.IntProperty1).Name("Caption1").TypeConverter(IntAllowThousands);
			
			//Or create an instance directly.
            Map(m => m.IntProperty2).Name("Caption2").TypeConverter(new BooleanConverter(new List<string> { "x", "xx" }, new List<string> { "o", "oo" });;
			
			//The third column use a very specific date format.
			Map(m => m.IntProperty1).Name("Caption3").TypeConverter(new DateTimeConverter(CultureInfo.GetCultureInfo("el"));
			
        }
    }

How To Use (attributes):

In addition to that you can now use attributes to set an instance of a converter instead only setting a converter type (I did not use this for myself. I thought it should be done for completeness. So I don't know if it's usable):

[TypeConverter( InstanceOfMyCustomTypeConverter ) )]
⚠️ **GitHub.com Fallback** ⚠️