【发布时间】:2020-02-25 16:27:58
【问题描述】:
我在使用 CsvHelper 将空(值为“NULL”)Key 字段转换为字节数组时遇到了一点问题,我不断收到此异常:
CsvHelper.ReaderException: An unexpected error occurred.
---> System.FormatException: Could not find any recognizable digits.
at System.ParseNumbers.StringToInt(ReadOnlySpan`1 s, Int32 radix, Int32 flags, Int32& currPos)
at System.Convert.ToByte(String value, Int32 fromBase)
at CsvHelper.TypeConversion.ByteArrayConverter.HexStringToByteArray(String hex)
at CsvHelper.TypeConversion.ByteArrayConverter.ConvertFromString(String text, IReaderRow row, MemberMapData memberMapData)
at lambda_method(Closure )
at CsvHelper.Expressions.RecordCreator.Create[T]()
at CsvHelper.Expressions.RecordManager.Create[T]()
at CsvHelper.CsvReader.GetRecord[T]()
--- End of inner exception stack trace ---
映射是这样设置的:
public sealed class ZaznamMapping : ClassMap<Zaznam>
{
public ZaznamMapping(FileSettings configuration)
{
var nullValues = new[] { "NULL", "null", string.Empty };
for (int i = 0; i < configuration.Count(); i++)
{
switch (configuration[i])
{
case Col.Ignore: continue;
case Col.Id: Map(m => m.Id).Index(i).TypeConverterOption.NullValues(nullValues); break;
case Col.Idd: Map(m => m.Idd).Index(i).TypeConverterOption.NullValues(nullValues); break;
case Col.Data: Map(m => m.Data).Index(i).TypeConverterOption.NullValues(nullValues); break;
case Col.Key: Map(m => m.Key).Index(i).TypeConverterOption.NullValues(nullValues); break;
default: throw new NotSupportedException($"Mapping() - Unknown column \"{configuration[i].ToString()}\"!");
}
}
}
}
还有扎兹南班:
public sealed class Zaznam
{
public int Id { get; set; }
public int Idd { get; set; }
public byte[] Data { get; set; }
public byte[] Key { get; set; }
}
文件Key coluimn 中的值实际上是NULL(如在包含字母“NULL”的字符串中)。 ByteArrayconverter 不应该尊重TypeConverterOptions吗?
问题是:
- 我做错了吗?
- 我应该自己制作转换器吗?
【问题讨论】:
-
但是你的 nullValues 数组也包含空字符串?