![](/img/trans.png)
[英]How do I find out which argument is invalid when receiving 'The best overloaded match for (…) has some invalid arguments'?
[英]How do I find out, which columns has invalid value
我正在使用 CsvHelper 来解析 CSV 文件,包括类型转换,例如到 DateTime。
当单元格值的格式无效时,我得到
while (CsvReader.Read())
{
var record = CsvReader.GetRecord<TModel>();
csvReader.GetRecord<MyModel>(); //throws following exception:
// CsvHelper.ReaderException: 'An unexpected error occurred.'
// Inner Exception
//
// FormatException: The string 'a' was not recognized as a valid DateTime. There is an unknown word starting at index '0'.
我如何找到无法解析的单元格的 HeaderName 或 MyModel 的属性。
你应该添加一个 try 并且在你的 catch 中你可以使用
int index = csvReader.Context.CurrentIndex; // zero based
int row = csvReader.Context.Row; // 1 based
然后您可以使用标题记录获取您的字段
csvReader.Context.HeaderRecord[index]
加起来它不工作,这就是我是怎么做的
catch (ReaderException e)
{
string notification = $"The field { e.ReadingContext.HeaderRecord[e.ReadingContext.CurrentIndex] } has the invalid value { e.ReadingContext.Record[e.ReadingContext.CurrentIndex] }";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.