I'm importing a csv of sales orders into CsvSalesOrder objects. The CsvSalesOrder has an Address property, and its properties are of a custom type called CustomString. How can I convert the strings into the CustomString types when importing them? My CustomString class:
public struct CustomString
{
public string Value { get; }
public CustomString(string val)
{
if (string.IsNullOrWhiteSpace(val))
{
Value = null;
}
else
{
Value = val.ToUpper();
}
}
public static implicit operator string(CustomString s) => s.Value;
public static explicit operator CustomString(string s) => new CustomString(s);
public override string ToString() => Value;
}
My mapping setup:
var config = new ChoCSVRecordConfiguration<T>()
.WithFirstLineHeader()
.Configure(c => c.ThrowAndStopOnMissingField = false)
.Configure(c => c.IgnoreEmptyLine = true)
.Configure(c => c.FileHeaderConfiguration.IgnoreColumnsWithEmptyHeader = true);
foreach (var header in headers)
{
if (mapping.TryGetValue(header, out var propName))
config.Map(propName, header);
}
My import code:
using var reader = new ChoCSVReader<T>(stream, config)
.WithMaxScanRows(2)
.QuoteAllFields()
.IgnoreFieldValueMode(ChoIgnoreFieldValueMode.Any);
return reader.AsTypedEnumerable<T>();
The Address fields are all null when I try to import. I tried adding a type converter as documented here, but it did not work.
ChoCSVRecordFieldConfiguration customStringConfig = new ChoCSVRecordFieldConfiguration("Address1");
customStringConfig.AddConverter(TypeDescriptor.GetConverter(typeof(CustomString)));
config.CSVRecordFieldConfigurations.Add(customStringConfig);