CsvHelper
repository·master·Indexed 26 days ago
https://github.com/joshclose/csvhelperA high-performance C# library for reading and writing CSV files, optimized for mapping CSV data to custom class objects. It supports asynchronous operations, class-level configuration attributes, custom object instantiation via IObjectResolver, and flexible quoting and delimiter detection.
What's inside CsvHelper
- CsvHelper is a library for reading and writing CSV files. It is designed to be extremely fast, flexible, and easy to use, with support for reading and writing custom class objects.
Initialize delegate argument objects via constructors
masterStarting from version 27, all delegate argument objects have had their
initaccessors removed. You must now use constructor parameters to initialize these objects instead of object initializers.Common examples include:
BadDataFoundArgsConvertFromStringArgsConvertToStringArgsGetConstructorArgsGetDynamicPropertyNameArgsHeaderValidatedArgsMissingFieldFoundArgsPrepareHeaderForMatchArgsReadingExceptionOccurredArgsReferenceHeaderPrefixArgsShouldQuoteArgsShouldSkipRecordArgsShouldUseConstructorParametersArgsValidateArgs
Use null for ConfigurationFunctions.ShouldSkipRecord in version 28+
masterThe
ConfigurationFunctions.ShouldSkipRecordhelper has been removed in version 28. If you want to disable skipping records or use the default behavior, you can now assignnullto theShouldSkipRecordproperty in yourCsvConfiguration.var config = new CsvConfiguration(CultureInfo.InvariantCulture) { ShouldSkipRecord = null };Update ValidateArgs constructor for version 30
masterWhen migrating from version 29 to version 30, the
ValidateArgsconstructor signature has changed. You must now include the row in addition to the field.// 29 var args = new ValidateArgs(field); // 30 var args = new ValidateArgs(field, row);Configure Delimiter Detection (v29.0.0+)
masterDelimiter detection has been moved to a configuration function. This allows you to provide custom logic for detecting the delimiter. The default logic is provided inConfigurationFunction.GetDelimiter.Configure CsvConfiguration using property setters
masterIn version 23 and later, all constructor parameters for
CsvConfigurationhave been removed. You must now use property setters to configure options. For example, instead of passing a delimiter to the constructor, set theDelimiterproperty on the instance.// v23 and later var config = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";", };Migrate CsvConfiguration.SanitizeForInjection to InjectionOptions
masterIn version 29, the boolean property
CsvConfiguration.SanitizeForInjectionwas replaced by theInjectionOptionsenum propertyCsvConfiguration.InjectionOptions. If you were previously settingSanitizeForInjection = true, you should now useInjectionOptions.Escapeto achieve similar behavior.// Version 28 style var config = new CsvConfiguration(CultureInfo.InvariantCulture) { SanitizeForInjection = true, } // Version 29 style var config = new CsvConfiguration(CultureInfo.InvariantCulture) { InjectionOptions = InjectionOptions.Escape, }Update ShouldQuote delegate signature
masterIn version 22, the
ShouldQuotedelegate signature inCsvConfigurationwas updated to include the row index. The new signature is(field, context, row) => bool.// v22 var config = new CsvConfiguration(CultureInfo.InvariantCulture) { ShouldQuote = (field, context, row) => true, };Configure case-insensitive Enum conversion
masterStarting with version 22,
EnumConverteris case-sensitive by default. To allow enums to ignore case during mapping, use theEnumIgnoreCase()type converter option.Map(m => m.Property).TypeConverterOption.EnumIgnoreCase();Upgrade to CsvHelper v33.1.0
masterVersion 33.1.0 includes performance improvements and updated framework support:
- Performance: Uses
Attributes.IsDefinedfor type attribute checks and includes an early exit inObjectCreatorwhen no arguments are present to reduce allocations. - Framework Support: Removed support for
net6.0andnet7.0. Added support fornet9.0. - Bug Fixes: Added
DisposeandDisposeAsyncto writer async methods. Fixed an issue whereUseDefaultOnConversionFailurefailed when the default value wasnull.
- Performance: Uses
Configure whitespace characters in CsvConfiguration
masterIn version 27 and later, the tab character (
\t) was removed from the defaultWhiteSpaceCharsarray. If your application requires tab characters to be treated as whitespace for trimming, you must explicitly include them in theWhiteSpaceCharsconfiguration.var config = new CsvConfiguration(CultureInfo.InvariantCulture) { WhiteSpaceChars = new[] { ' ', '\t' }, };Install CsvHelper
masterYou can install CsvHelper using either the NuGet Package Manager Console or the .NET CLI.
### Package Manager Console ```powershell PM> Install-Package CsvHelper.NET CLI Console
> dotnet add package CsvHelper