如何使用 FileHelper 库获取分隔符的数量
How to use FileHelper library to get number of delimiters
使用 FileHelper 库如果记录在一行中没有预期的分隔符数量,是否有办法获得异常?
另一个问题是,如果某个特定字段(例如名称)超过预期长度,是否有办法获得异常?
如果我将名称设置为 maxLength 30,如果它超过 30,我会得到一个异常。
或者某个字段根本不等于预期长度?
如果记录在一行中没有预期的分隔符数量,则会出现异常
如果行中没有 enough/too 多个字段,则抛出异常是 FileHelpers 的默认行为,因此无需执行任何特殊操作即可获得它:
[DelimitedRecord(",")]
public class Test
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
public int SomeInt1 { get; set; }
public override string ToString() =>
$"{SomeInt} - {SomeString} - {SomeInt1}";
}
var result = new FileHelperEngine<Test>()
.ReadString(@"123,That's the string")
.Single();
Console.WriteLine(result);
将导致
FileHelpers.FileHelpersException: Line: 1 Column: 4. Delimiter ',' not
found after field 'k__BackingField' (the record has less
fields, the delimiter is wrong or the next field must be marked as
optional). at
FileHelpers.DelimitedField.BasicExtractString(LineInfo line) at
FileHelpers.DelimitedField.ExtractFieldString(LineInfo line) at
FileHelpers.FieldBase.ExtractFieldValue(LineInfo line) at
FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo
line, Object[] values) at
FileHelpers.FileHelperEngine`1.ReadStreamAsList(TextReader reader,
Int32 maxRecords, DataTable dt)
如果某个特定字段(例如 Name)超过预期长度,是否有办法获得异常
据我所知,FileHelpers 不支持开箱即用的标准 DataAnnotations(或者它实际上可以支持它,但由于 https://github.com/dotnet/standard/issues/450 而无法正常工作),因此您可能不得不手动添加验证
所以,你安装 https://www.nuget.org/packages/System.ComponentModel.Annotations/4.4.0
然后通过将 [StringLength(maximumLength: 5)]
添加到模型 SomeString
属性
[DelimitedRecord(",")]
public class Test
{
public int SomeInt { get; set; }
[StringLength(maximumLength: 5)]
public string SomeString { get; set; }
public int SomeInt1 { get; set; }
public override string ToString() =>
$"{SomeInt} - {SomeString} - {SomeInt1}";
}
有
var result = new FileHelperEngine<Test>()
.ReadString(@"123,That's the string, 456")
.Single();
Console.WriteLine(result);
var context = new ValidationContext(result, serviceProvider: null, items: null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(result, context, results, validateAllProperties: true);
if (!isValid)
{
Console.WriteLine("Not valid");
foreach (var validationResult in results)
{
Console.WriteLine(validationResult.ErrorMessage);
}
}
您将得到以下输出
Not valid
The field SomeString must be a string with a maximum length of 5.
使用 FileHelper 库如果记录在一行中没有预期的分隔符数量,是否有办法获得异常?
另一个问题是,如果某个特定字段(例如名称)超过预期长度,是否有办法获得异常?
如果我将名称设置为 maxLength 30,如果它超过 30,我会得到一个异常。
或者某个字段根本不等于预期长度?
如果记录在一行中没有预期的分隔符数量,则会出现异常
如果行中没有 enough/too 多个字段,则抛出异常是 FileHelpers 的默认行为,因此无需执行任何特殊操作即可获得它:
[DelimitedRecord(",")]
public class Test
{
public int SomeInt { get; set; }
public string SomeString { get; set; }
public int SomeInt1 { get; set; }
public override string ToString() =>
$"{SomeInt} - {SomeString} - {SomeInt1}";
}
var result = new FileHelperEngine<Test>()
.ReadString(@"123,That's the string")
.Single();
Console.WriteLine(result);
将导致
FileHelpers.FileHelpersException: Line: 1 Column: 4. Delimiter ',' not found after field 'k__BackingField' (the record has less fields, the delimiter is wrong or the next field must be marked as optional). at FileHelpers.DelimitedField.BasicExtractString(LineInfo line) at FileHelpers.DelimitedField.ExtractFieldString(LineInfo line) at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line) at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values) at FileHelpers.FileHelperEngine`1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)
如果某个特定字段(例如 Name)超过预期长度,是否有办法获得异常
据我所知,FileHelpers 不支持开箱即用的标准 DataAnnotations(或者它实际上可以支持它,但由于 https://github.com/dotnet/standard/issues/450 而无法正常工作),因此您可能不得不手动添加验证
所以,你安装 https://www.nuget.org/packages/System.ComponentModel.Annotations/4.4.0
然后通过将 [StringLength(maximumLength: 5)]
添加到模型 SomeString
属性
[DelimitedRecord(",")]
public class Test
{
public int SomeInt { get; set; }
[StringLength(maximumLength: 5)]
public string SomeString { get; set; }
public int SomeInt1 { get; set; }
public override string ToString() =>
$"{SomeInt} - {SomeString} - {SomeInt1}";
}
有
var result = new FileHelperEngine<Test>()
.ReadString(@"123,That's the string, 456")
.Single();
Console.WriteLine(result);
var context = new ValidationContext(result, serviceProvider: null, items: null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject(result, context, results, validateAllProperties: true);
if (!isValid)
{
Console.WriteLine("Not valid");
foreach (var validationResult in results)
{
Console.WriteLine(validationResult.ErrorMessage);
}
}
您将得到以下输出
Not valid
The field SomeString must be a string with a maximum length of 5.