Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.75 KB

README.md

File metadata and controls

56 lines (41 loc) · 1.75 KB

License: MIT NuGet version

TinyCsvParser.Enums

Adding support for parsing enums.

Supports .NET Core (.NET Standard 2+)

Installation

PM> Install-Package TinyCsvParser.Enums

Usage

The only thing you need to keep in mind when using this extension is that your mapping class must have a constructor taking in an instance of ITypeConverterProvider and passing it on to its base constructor. See example below.

// Entity
public enum TestEnum { A, B, C }

public class Data
{
    public TestEnum Value { get; set; }
}

// Mapping
private class CsvDataMapping : CsvMapping<Data>
{
    // Need to take in ITypeConverterProvider
    public CsvDataMapping(ITypeConverterProvider typeConverterProvider) : base(typeConverterProvider)
    {
        MapProperty(0, x => x.Value);
    }
}

// Parsing
var options = new CsvParserOptions(skipHeader: false, fieldsSeparator: ',');
var typeConverterProvider = new TypeConverterProvider().AddEnums(); // <-- This line
var parser = new CsvParser<Data>(options, new CsvDataMapping(typeConverterProvider));
var readerOptions = new CsvReaderOptions(new[] { ";" });
var result = parser.ReadFromString(readerOptions, $"A").ToList();

Console.WriteLine(result[0].Result.Value.ToString()); // Prints A

License

This project is licensed under the MIT license. See the LICENSE file for more info.