Replies: 5 comments
-
Can you give me example code showing this failing? |
Beta Was this translation helpful? Give feedback.
-
using System.Globalization;
using System.IO;
using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
this.AutoMap(new CsvConfiguration(CultureInfo.InvariantCulture)
{
PrepareHeaderForMatch = (header, index) => header.ToLowerInvariant()
});
}
}
public class Foo
{
public string Id { get; set; }
public string Name { get; set; }
}
[TestClass]
public class CsvHelperTests
{
[TestMethod]
public void Reproduce_issue_1511()
{
using var reader = new StringReader(@"id,name
1,one
2,two");
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
csv.Configuration.RegisterClassMap<FooMap>();
var records = csv.GetRecords<Foo>();
}
} |
Beta Was this translation helpful? Give feedback.
-
Do you need to use The constructor overload of The reason it's not working is because you're passing a new configuration into the Here is the intended way to use this. void Main()
{
var s = new StringBuilder();
s.AppendLine("id,name");
s.AppendLine("1,one");
s.AppendLine("2,two");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.PrepareHeaderForMatch = (header, index) => header.ToLowerInvariant();
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for explanation @JoshClose. Does that mean that PrepareHeaderForMatch (or anything else that is set in the configuration class) can't be set via ClassMap? |
Beta Was this translation helpful? Give feedback.
-
It can be, but you should pass an existing instance into the map then. void Main()
{
var s = new StringBuilder();
s.AppendLine("id,name");
s.AppendLine("1,one");
s.AppendLine("2,two");
// Create shared config.
var config = new CsvConfiguration(CultureInfo.InvariantCulture);
using (var reader = new StringReader(s.ToString()))
// Pass config into CsvReader.
using (var csv = new CsvReader(reader, config))
{
// Create map instance using config.
var map = new FooMap(config);
// Register map instance.
csv.Configuration.RegisterClassMap(map);
csv.GetRecords<Foo>().ToList().Dump();
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap(CsvConfiguration config)
{
config.PrepareHeaderForMatch = (header, index) => header.ToLowerInvariant();
Map(m => m.Id);
Map(m => m.Name);
}
} |
Beta Was this translation helpful? Give feedback.
-
When I register a Func for PrepareHeaderForMatch via CsvReader.Configuration, it's all good. Once I move it to a class map and add via AutoMap, it doesn't work anymore. The ClassMap is initialized, it's just the Func is not called.
CsvHelper v15.0.5
Beta Was this translation helpful? Give feedback.
All reactions