I mapped a csv file in a class, suppose it's called A made this way
public class A {
private string field1;
private string field2;
}
When I invoke the writeRecords method I would like to reverse the order of the fields eg I would like to write field 2 first and then field 1 it's possible?
You can create a custom ClassMap<A>
and provide the indexes for each property:
public class A_Mapper : ClassMap<A>
{
public A_Mapper()
{
Map(a => a.field1).Index(1);
Map(a => a.field2).Index(0);
}
}
To make it work you need to register this mapper to the configuration:
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
using (CsvWriter csv = new CsvWriter(writer))
{
csv.Configuration.RegisterClassMap<A_Mapper>();
csv.WriteRecords(aList);
}
Documentation: https://joshclose.github.io/CsvHelper/2.x/