why cant i convert from 'System.IO.StreamWriter' to 'CsvHelper.ISerializer'?

岁酱吖の 提交于 2020-03-01 02:09:38

问题


Trying to write the contents of people to a CSVfile and then export it, however i'm getting a build error and its failing. the error is:

cannot convert from 'System.IO.StreamWriter' to 'CsvHelper.ISerializer'

Not sure why this is happening unless as i'm certain i've done it this way loads of times.

private void ExportAsCSV()
{
    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream))
        {
            using (var csv = new CsvHelper.CsvWriter(writer))
            {
                csv.WriteRecords(people);
            }

            var arr = memoryStream.ToArray();
            js.SaveAs("people.csv",arr);
        }
    }
}

回答1:


There was a breaking change with version 13.0.0. There have been many issues with localization, so @JoshClose is requiring users to specify the CultureInfo they want to use. You now need to include CultureInfo when creating CsvReader and CsvWriter. https://github.com/JoshClose/CsvHelper/issues/1441

private void ExportAsCSV()
{
    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream))
        {
            using (var csv = new CsvHelper.CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture)
            {
                csv.WriteRecords(people);
            }

            var arr = memoryStream.ToArray();
            js.SaveAs("people.csv",arr);
        }
    }
}

Note: CultureInfo.CurrentCulture was the default in previous versions.

Consider

  • CultureInfo.InvariantCulture - If you control both the writing and the reading of the file. That way it will work no matter what culture the user has on their computer.
  • CultureInfo.CreateSpecificCulture("en-US") - If you need it to work for a particular culture, independent of the user's culture.


来源:https://stackoverflow.com/questions/59787783/why-cant-i-convert-from-system-io-streamwriter-to-csvhelper-iserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!