I use CsvReader occasionally; when upgrading from v18 to the latest v22 via NuGet I found breaking changes but it's not clear from the CsvReader's site how to fix breaking changes. Does anyone know how to provide equivalent functionality in CsvHelper v22 to do this code from v18:
using (var csv = new CsvHelper.CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<MyMap>(); //I believe this should now be csv.Context.RegisterClassMap<MyMap>();
csv.Configuration.HeaderValidated = null; //broken - no setter on IReaderConfiguration
csv.Configuration.MissingFieldFound = null; //broken - no setter on IReaderConfiguration
}
The examples on CsvHelper website, StackOverflow and the documentation still reference old v18 setters so I can't find the way around it.
They're init-only properties, so they can still be set in an initializer:
var conf = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HeaderValidated = ... ,
MissingFieldFound = ...
};
using (var csv = new CsvHelper.CsvReader(reader, conf))
{
csv.Context.RegisterClassMap<SomeMap>();
}
There's also a massive constructor with a bunch of optionals, you could pick them out :
var conf = new CsvConfiguration(CultureInfo.InvariantCulture,
headerValidated: ...,
missingFieldFound: ...
);
The constructor signature seems to indicate they'll end up null by default if this constructor is used and nothing is spec'd for them..
..but then there was one recent related issue with this constructor because null
cannot be supplied to mean "take no action" because it's the default value used to indicate "no value was supplied" (and I'm not sure if a optional delegate can ever be anything else tbh) so any null
you pass in is effectively ignored and the default function in ConfigurationFunctions.cs is used..
The suggested workaround is to supply a "does-nothing" delegate if you want to use the constructor form and want "no action" to arise:
var conf = new CsvConfiguration(CultureInfo.InvariantCulture,
headerValidated: (_,_) => { },
missingFieldFound: (_,_,_) => { }
);
Personally I'd use the initializer form