Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created April 3, 2020 14:31
Show Gist options
  • Save JoshClose/3c0d669fe79f0d5d3cbdcb022b783dd6 to your computer and use it in GitHub Desktop.
Save JoshClose/3c0d669fe79f0d5d3cbdcb022b783dd6 to your computer and use it in GitHub Desktop.
void Main()
{
var s = new StringBuilder();
s.AppendLine("Id,Name,A,B,C");
s.AppendLine("1,one,9,8,7");
s.AppendLine("2,two,6,5,4");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<FooMap>();
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? Optional { get; set; }
public Dictionary<string, int> TheRest { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.Id);
Map(m => m.Name);
Map(m => m.Optional).Optional();
Map(m => m.TheRest).Index(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment