Migration guide
Migrating to v16.0.0
Moving to v16.0.0 from v15.0.10. Changelog.
CsvHelper 16 adds GetDynamicPropertyName, a setting that names the properties of the records GetRecords<dynamic>()
returns. With it you can read a file whose header has the same name twice, which failed before, since both columns
got the same property name. The names stay the same unless you set it. Code that implements IReaderConfiguration
needs the new property.
Dynamic record property names
In 15, a dynamic record's property names were the header names passed through PrepareHeaderForMatch, so making
them unique meant changing how every header is matched, for class records too. In 16, GetDynamicPropertyName names
them, and changes only dynamic records:
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
PrepareHeaderForMatch = (header, index) => header + index,
};
using var csv = new CsvReader(reader, config);
var records = csv.GetRecords<dynamic>().ToList();
// Name,Name,Age gives Name0, Name1 and Age2var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
GetDynamicPropertyName = (context, index) =>
{
var header = context.HeaderRecord[index];
var count = context.HeaderRecord.Take(index).Count(h => h == header);
return count == 0 ? header : header + (count + 1);
},
};
using var csv = new CsvReader(reader, config);
var records = csv.GetRecords<dynamic>().ToList();
// Name,Name,Age gives Name, Name2 and Age- The default is
ConfigurationFunctions.GetDynamicPropertyName, which names them as 15 did: the header name passed throughPrepareHeaderForMatch, orField1,Field2and so on when there is no header. - Without a header,
context.HeaderRecordisnull: the function above is for files with one.
All API changes
Every change to the public API since v15.0.10: 3 added. Open a type to see its changes.
ConfigurationFunctionsstatic class1 added
Added
CsvConfigurationclass1 added
Added
IReaderConfigurationinterface1 added
Added