CsvHelper

repository·master·Indexed 26 days ago

https://github.com/joshclose/csvhelper

A 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.

Tokens
8.4K
Snippets
31
Records
81
Agent score
89%

What's inside CsvHelper

  1. Initialize delegate argument objects via constructors

    master

    Starting from version 27, all delegate argument objects have had their init accessors removed. You must now use constructor parameters to initialize these objects instead of object initializers.

    Common examples include:

    • BadDataFoundArgs
    • ConvertFromStringArgs
    • ConvertToStringArgs
    • GetConstructorArgs
    • GetDynamicPropertyNameArgs
    • HeaderValidatedArgs
    • MissingFieldFoundArgs
    • PrepareHeaderForMatchArgs
    • ReadingExceptionOccurredArgs
    • ReferenceHeaderPrefixArgs
    • ShouldQuoteArgs
    • ShouldSkipRecordArgs
    • ShouldUseConstructorParametersArgs
    • ValidateArgs
  2. Configure CsvConfiguration using property setters

    master

    In version 23 and later, all constructor parameters for CsvConfiguration have been removed. You must now use property setters to configure options. For example, instead of passing a delimiter to the constructor, set the Delimiter property on the instance.

    // v23 and later
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    	Delimiter = ";",
    };
  3. Migrate CsvConfiguration.SanitizeForInjection to InjectionOptions

    master

    In version 29, the boolean property CsvConfiguration.SanitizeForInjection was replaced by the InjectionOptions enum property CsvConfiguration.InjectionOptions. If you were previously setting SanitizeForInjection = true, you should now use InjectionOptions.Escape to 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,
    }
  4. Upgrade to CsvHelper v33.1.0

    master

    Version 33.1.0 includes performance improvements and updated framework support:

    • Performance: Uses Attributes.IsDefined for type attribute checks and includes an early exit in ObjectCreator when no arguments are present to reduce allocations.
    • Framework Support: Removed support for net6.0 and net7.0. Added support for net9.0.
    • Bug Fixes: Added Dispose and DisposeAsync to writer async methods. Fixed an issue where UseDefaultOnConversionFailure failed when the default value was null.
  5. Configure whitespace characters in CsvConfiguration

    master

    In version 27 and later, the tab character (\t) was removed from the default WhiteSpaceChars array. If your application requires tab characters to be treated as whitespace for trimming, you must explicitly include them in the WhiteSpaceChars configuration.

    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    	WhiteSpaceChars = new[] { ' ', '\t' },
    };