CsvHelper
CsvHelper copied to clipboard
Execption when `Delimiter` is initialized from other string variable and not a string litteral
Describe the bug
When CsvConfiguration.Delimiter
is initialized with ";"
things work, but if it is initialized from a variable like public static string Delimiter { get; set; } = ";";
then the program crashes later on.
To Reproduce
internal class CsvManager
{
public static string Delimiter { get; set; } = ";";
private static CsvConfiguration GetCsvConfiguration()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
// Delimiter = Delimiter // exception
Delimiter = ";" // works
};
return config;
}
public static List<SomeData> ReadData(string path)
{
using var reader = new StreamReader(path);
using var csv = new CsvReader(reader, GetCsvConfiguration());
return csv.GetRecords<SomeData>().ToList();
}
public static void WriteData(string path, IEnumerable<SomeData> data)
{
using var writer = new StreamWriter(path);
using var csv = new CsvWriter(writer, GetCsvConfiguration());
csv.WriteRecords(data);
}
}
When calling WriteData
and having Delimiter = Delimiter
active the program crashes with the following stacktrace:
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.SelectMany[TSource,TResult](IEnumerable`1 source, Func`2 selector)
at CsvHelper.CsvWriter..ctor(TextWriter writer, IWriterConfiguration configuration, Boolean leaveOpen)
at ....CsvManager.WriteData(String path, IEnumerable`1 data) in ...\CsvManager.cs:line 35
Same behaviour on versions 27.2.1 and 30.0.1.