CsvClassMap not found error in visual studio 2015

Viewed 6917

I am trying to use CsvHelper library in one of my projects. its just console application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;
using Newtonsoft.Json;
using CsvHelper.TypeConversion;


namespace csvtest4
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> result = new List<string>();
            string value;
            using (TextReader fileReader = File.OpenText("list.csv"))
            {
                var csv = new CsvReader(fileReader);
                csv.Configuration.HasHeaderRecord = true;
                csv.Configuration.RegisterClassMap<CustomClassMap>();
                while (csv.Read())
                {
                    for (int i = 0; csv.TryGetField<string>(i, out value); i++)
                    {
                        result.Add(value);
                    }
                }

               var json = JsonConvert.SerializeObject(result);
                Console.WriteLine(json.ToString());
            }

        }

    }

    public class CustomClassMap : CsvClassMap<TestModel>
    {
        public CustomClassMap()
        {
            Map(m => m.StringProperty).Index(0);
            Map(m => m.GuidProperty).Index(1);
            Map(m => m.IntProperty).Index(2).TypeConverter<MyCustomTypeConverter>();
        }
    }
}

Not sure, what I am missing but my vs2015 keeps complaining about CsvClassMap its saying type or namespace not found, are you missing directives. Also, it's not recognizing map function either

I think I added required namespace as other CsvHelper function working fine

3 Answers

CsvClassMap is no longer in the latest versions of CsvHelper.

Use ClassMap<Model> instead

CsvHelper version 20.0.0 moved RegisterClassMap from the Configuration object to the reader/writer Context object.

var Writer = new CsvWriter(...);
Writer.Context.RegisterClassMap<T>(mymap); // not in Configuration

Ref: https://joshclose.github.io/CsvHelper/change-log/

Related