Migration guide
Migrating to v7.0.0
Moving to v7.0.0 from v6.1.1. Changelog.
CsvHelper 7 makes reading faster. To get there, the reading and writing state is used as classes: the
IReadingContext and IWritingContext interfaces are removed, and everything that took or returned one uses
ReadingContext or WritingContext. You're affected if your code names the interfaces: callbacks written as methods,
your own exceptions, parsers or serializers, and test doubles. Code that only uses lambdas and the contexts' members
compiles unchanged. The target frameworks don't change: .NET Standard 2.0 and .NET Framework 4.5.
Reading and writing contexts
Replace IReadingContext with ReadingContext, and IWritingContext with WritingContext. Lambdas need no change,
since their parameter types are inferred; methods that you assign to a callback do.
public static class CsvCallbacks
{
public static void OnBadData(IReadingContext context)
{
Console.WriteLine($"Bad data on row {context.Row}: {context.RawRecord}");
}
public static void OnMissingField(string[] headerNames, int index, IReadingContext context)
{
Console.WriteLine($"Missing field '{headerNames[0]}' on row {context.Row}.");
}
}
using var csv = new CsvReader(reader);
csv.Configuration.BadDataFound = CsvCallbacks.OnBadData;
csv.Configuration.MissingFieldFound = CsvCallbacks.OnMissingField;public static class CsvCallbacks
{
public static void OnBadData(ReadingContext context)
{
Console.WriteLine($"Bad data on row {context.Row}: {context.RawRecord}");
}
public static void OnMissingField(string[] headerNames, int index, ReadingContext context)
{
Console.WriteLine($"Missing field '{headerNames[0]}' on row {context.Row}.");
}
}
using var csv = new CsvReader(reader);
csv.Configuration.BadDataFound = CsvCallbacks.OnBadData;
csv.Configuration.MissingFieldFound = CsvCallbacks.OnMissingField;| CsvHelper 6 | CsvHelper 7 |
|---|---|
BadDataFound, HeaderValidated and MissingFieldFound get an IReadingContext |
They get a ReadingContext |
Context of CsvReader, CsvParser, CsvFieldReader, IReaderRow, IParser and IFieldReader is an IReadingContext |
A ReadingContext |
Context of CsvWriter, CsvSerializer, IWriterRow and ISerializer is an IWritingContext |
A WritingContext |
CsvHelperException.ReadingContext and WritingContext |
Typed ReadingContext and WritingContext |
The constructors of CsvHelperException and the exceptions derived from it take an IReadingContext or IWritingContext |
They take a ReadingContext or WritingContext |
- Your own exceptions: an exception derived from
CsvHelperExceptionpasses aReadingContextorWritingContextto the base constructor. - Your own parser or serializer:
IParser.ContextandISerializer.Contextreturn the classes, so yours returns aReadingContextorWritingContext. - Test doubles: there are no interfaces to mock. Create a real context:
new ReadingContext(textReader, configuration, leaveOpen)ornew WritingContext(textWriter, configuration, leaveOpen). ReadingContextholds fields: the reading state (Row,RawRow,Record,HeaderRecord,CurrentIndex,ColumnCount, the buffer and its positions) is in public fields instead of virtual properties, andRawRecord,Field,ParserConfigurationandReaderConfigurationare no longervirtual. Reading them looks the same, but a class derived fromReadingContextcan't override them.ReadingContext.Cis gone: the character the parser is on is private to the parser now.
All API changes
Every change to the public API since v6.1.1: 36 added, 46 changed, 39 removed (85 breaking). Open a type to see its changes.
CsvHelper
BadDataExceptionclass3 breaking1 removed2 changed1 added
BadDataException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public BadDataException(IReadingContext context, string message)public BadDataException(ReadingContext context, string message)BadDataException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public BadDataException(IReadingContext context, string message, Exception innerException)public BadDataException(ReadingContext context, string message, Exception innerException)Removed
Added
CsvFieldReaderclass1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
public virtual IReadingContext Context { get; }public virtual ReadingContext Context { get; }CsvHelperExceptionclass8 breaking2 removed6 changed2 added
CsvHelperException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public CsvHelperException(IReadingContext context, string message)public CsvHelperException(ReadingContext context, string message)CsvHelperException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public CsvHelperException(IReadingContext context, string message, Exception innerException)public CsvHelperException(ReadingContext context, string message, Exception innerException)CsvHelperException(WritingContext, string)Breaking
The parameters changed from (IWritingContext, string) to (WritingContext, string).
public CsvHelperException(IWritingContext context, string message)public CsvHelperException(WritingContext context, string message)CsvHelperException(WritingContext, string, Exception)Breaking
The parameters changed from (IWritingContext, string, Exception) to (WritingContext, string, Exception).
public CsvHelperException(IWritingContext context, string message, Exception innerException)public CsvHelperException(WritingContext context, string message, Exception innerException)ReadingContextBreaking
The type changed from IReadingContext to ReadingContext.
public IReadingContext ReadingContext { get; }public ReadingContext ReadingContext { get; }WritingContextBreaking
The type changed from IWritingContext to WritingContext.
public IWritingContext WritingContext { get; }public WritingContext WritingContext { get; }Removed
Added
CsvParserclass1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
public virtual IReadingContext Context { get; }public virtual ReadingContext Context { get; }CsvReaderclass1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
public virtual IReadingContext Context { get; }public virtual ReadingContext Context { get; }CsvSerializerclass1 breaking1 changed
ContextBreaking
The type changed from IWritingContext to WritingContext.
public virtual IWritingContext Context { get; }public virtual WritingContext Context { get; }CsvWriterclass1 breaking1 changed
ContextBreaking
The type changed from IWritingContext to WritingContext.
public virtual IWritingContext Context { get; }public virtual WritingContext Context { get; }IFieldReaderinterface1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
IReadingContext Context { get; }ReadingContext Context { get; }IParserinterface1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
IReadingContext Context { get; }ReadingContext Context { get; }IReaderRowinterface1 breaking1 changed
ContextBreaking
The type changed from IReadingContext to ReadingContext.
IReadingContext Context { get; }ReadingContext Context { get; }ISerializerinterface1 breaking1 changed
ContextBreaking
The type changed from IWritingContext to WritingContext.
IWritingContext Context { get; }WritingContext Context { get; }IWriterRowinterface1 breaking1 changed
ContextBreaking
The type changed from IWritingContext to WritingContext.
IWritingContext Context { get; }WritingContext Context { get; }MissingFieldExceptionclass3 breaking1 removed2 changed1 added
MissingFieldException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public MissingFieldException(IReadingContext context, string message)public MissingFieldException(ReadingContext context, string message)MissingFieldException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public MissingFieldException(IReadingContext context, string message, Exception innerException)public MissingFieldException(ReadingContext context, string message, Exception innerException)Removed
Added
ParserExceptionclass3 breaking1 removed2 changed1 added
ParserException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public ParserException(IReadingContext context, string message)public ParserException(ReadingContext context, string message)ParserException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public ParserException(IReadingContext context, string message, Exception innerException)public ParserException(ReadingContext context, string message, Exception innerException)Removed
Added
ReaderExceptionclass3 breaking1 removed2 changed1 added
ReaderException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public ReaderException(IReadingContext context, string message)public ReaderException(ReadingContext context, string message)ReaderException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public ReaderException(IReadingContext context, string message, Exception innerException)public ReaderException(ReadingContext context, string message, Exception innerException)Removed
Added
ReadingContextclass34 breaking29 removed5 changed28 added
ReadingContextBreaking
The type no longer implements IReadingContext.
public class ReadingContext : IReadingContext, IDisposablepublic class ReadingContext : IDisposableFieldBreaking
No longer virtual: derived classes can't override it.
public virtual string Field { get; }public string Field { get; }ParserConfigurationBreaking
No longer virtual: derived classes can't override it.
public virtual IParserConfiguration ParserConfiguration { get; }public IParserConfiguration ParserConfiguration { get; }RawRecordBreaking
No longer virtual: derived classes can't override it.
public virtual string RawRecord { get; }public string RawRecord { get; }ReaderConfigurationBreaking
No longer virtual: derived classes can't override it.
public virtual IReaderConfiguration ReaderConfiguration { get; }public IReaderConfiguration ReaderConfiguration { get; }Removed
Added
ValidationExceptionclass3 breaking1 removed2 changed1 added
ValidationException(ReadingContext, string)Breaking
The parameters changed from (IReadingContext, string) to (ReadingContext, string).
public ValidationException(IReadingContext context, string message)public ValidationException(ReadingContext context, string message)ValidationException(ReadingContext, string, Exception)Breaking
The parameters changed from (IReadingContext, string, Exception) to (ReadingContext, string, Exception).
public ValidationException(IReadingContext context, string message, Exception innerException)public ValidationException(ReadingContext context, string message, Exception innerException)Removed
Added
WriterExceptionclass3 breaking1 removed2 changed1 added
WriterException(WritingContext, string)Breaking
The parameters changed from (IWritingContext, string) to (WritingContext, string).
public WriterException(IWritingContext context, string message)public WriterException(WritingContext context, string message)WriterException(WritingContext, string, Exception)Breaking
The parameters changed from (IWritingContext, string, Exception) to (WritingContext, string, Exception).
public WriterException(IWritingContext context, string message, Exception innerException)public WriterException(WritingContext context, string message, Exception innerException)Removed
Added
WritingContextclass1 breaking1 changed
WritingContextBreaking
The type no longer implements IWritingContext.
public class WritingContext : IWritingContext, IDisposablepublic class WritingContext : IDisposableRemoved types (2)
CsvHelper.Configuration
Configurationclass3 breaking3 changed
BadDataFoundBreaking
The type changed from Action<IReadingContext> to Action<ReadingContext>.
public virtual Action<IReadingContext> BadDataFound { get; set; }public virtual Action<ReadingContext> BadDataFound { get; set; }HeaderValidatedBreaking
The type changed from Action<bool, string[], int, IReadingContext> to Action<bool, string[], int, ReadingContext>.
public virtual Action<bool, string[], int, IReadingContext> HeaderValidated { get; set; }public virtual Action<bool, string[], int, ReadingContext> HeaderValidated { get; set; }MissingFieldFoundBreaking
The type changed from Action<string[], int, IReadingContext> to Action<string[], int, ReadingContext>.
public virtual Action<string[], int, IReadingContext> MissingFieldFound { get; set; }public virtual Action<string[], int, ReadingContext> MissingFieldFound { get; set; }IParserConfigurationinterface1 breaking1 changed
BadDataFoundBreaking
The type changed from Action<IReadingContext> to Action<ReadingContext>.
Action<IReadingContext> BadDataFound { get; set; }Action<ReadingContext> BadDataFound { get; set; }IReaderConfigurationinterface2 breaking2 changed
HeaderValidatedBreaking
The type changed from Action<bool, string[], int, IReadingContext> to Action<bool, string[], int, ReadingContext>.
Action<bool, string[], int, IReadingContext> HeaderValidated { get; set; }Action<bool, string[], int, ReadingContext> HeaderValidated { get; set; }MissingFieldFoundBreaking
The type changed from Action<string[], int, IReadingContext> to Action<string[], int, ReadingContext>.
Action<string[], int, IReadingContext> MissingFieldFound { get; set; }Action<string[], int, ReadingContext> MissingFieldFound { get; set; }CsvHelper.TypeConversion
TypeConverterExceptionclass6 breaking6 changed
TypeConverterException(ITypeConverter, MemberMapData, object, WritingContext)Breaking
The parameters changed from (ITypeConverter, MemberMapData, object, IWritingContext) to (ITypeConverter, MemberMapData, object, WritingContext).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, IWritingContext context)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, WritingContext context)TypeConverterException(ITypeConverter, MemberMapData, object, WritingContext, string)Breaking
The parameters changed from (ITypeConverter, MemberMapData, object, IWritingContext, string) to (ITypeConverter, MemberMapData, object, WritingContext, string).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, IWritingContext context, string message)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, WritingContext context, string message)TypeConverterException(ITypeConverter, MemberMapData, object, WritingContext, string, Exception)Breaking
The parameters changed from (ITypeConverter, MemberMapData, object, IWritingContext, string, Exception) to (ITypeConverter, MemberMapData, object, WritingContext, string, Exception).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, IWritingContext context, string message, Exception innerException)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, object value, WritingContext context, string message, Exception innerException)TypeConverterException(ITypeConverter, MemberMapData, string, ReadingContext)Breaking
The parameters changed from (ITypeConverter, MemberMapData, string, IReadingContext) to (ITypeConverter, MemberMapData, string, ReadingContext).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, IReadingContext context)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, ReadingContext context)TypeConverterException(ITypeConverter, MemberMapData, string, ReadingContext, string)Breaking
The parameters changed from (ITypeConverter, MemberMapData, string, IReadingContext, string) to (ITypeConverter, MemberMapData, string, ReadingContext, string).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, IReadingContext context, string message)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, ReadingContext context, string message)TypeConverterException(ITypeConverter, MemberMapData, string, ReadingContext, string, Exception)Breaking
The parameters changed from (ITypeConverter, MemberMapData, string, IReadingContext, string, Exception) to (ITypeConverter, MemberMapData, string, ReadingContext, string, Exception).
public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, IReadingContext context, string message, Exception innerException)public TypeConverterException(ITypeConverter typeConverter, MemberMapData memberMapData, string text, ReadingContext context, string message, Exception innerException)