I'm trying to use the ConvertUsing method of the CsvHelper library (v 2.4.0).
I've read the documentation about ConvertUsing but can't get it to work.
I'm using a simple class:
public class Test
{
public long Id { get; set; }
public string Title { get; set; }
}
With this ClassMap:
public class TestClassMap : CsvClassMap<Test>
{
public override void CreateMap()
{
Map(m => m.Id).Name("id").ConvertUsing(row => 11111);
Map(m => m.Title).Name("title").ConvertUsing(row => row.GetField("title") + " 123");
}
}
My code which uses these creates an instance of the class and then writes it to CSV:
var test = new Test() { Id = 99, Title = "Test title" };
using (var streamWriter = new StreamWriter("test.csv"))
{
var csv = new CsvWriter(streamWriter);
csv.Configuration.RegisterClassMap<TestClassMap>();
csv.WriteRecord(test);
}
However the output file test.csv is always the following format:
id,title
99,Test title
The output I'm looking for is:
id,title
11111,Test title 123
And the ConvertUsing is being ignored. I've tried only converting the Id, and only the Title, but this doesn't work either.
Any ideas where I'm going wrong?