"RecordCondition.ExcludeIfMatchRegex"的使用



库版本:v2.0.0.0

我想使用ExcludeIfMatchRegex来排除输入文件中的某些行。

我已经测试了下一个代码,但系统显示了常见的消息错误对象引用未设置为对象实例。如果我去掉包含";条件记录";,系统读取文件并返回通常的验证消息。

using FileHelpers;
using System;
[IgnoreEmptyLines()]
[ConitionalRecord(RecordCondition.ExcludeIfMatchRegex, "[0-9 A-Za-z.,]{1}S[0-9 A-Za-z.,]{10}")] 
[FixedLengthRecord(FixedMode.ExactLength)]
public sealed class PurchaseOrder : INotifyRead
{
[FieldFixedLength(1)]
[FieldTrim(TrimMode.Both)]
public string C;

[FieldFixedLength(1)]
[FieldTrim(TrimMode.Both)]
public string A;

[FieldFixedLength(10)]
[FieldTrim(TrimMode.Both)]
public string item;
public void AfterRead(EngineBase engine, string line)
{
// not exist the property "SkipThisRecord"??
}
}

看起来像是2.0.0.0库中的一个小错误。

当FileHelpers引擎读取一个文件,但所有行都被排除,并且类被INotifyRead修饰时,它会抛出对象引用错误。

但是,您可以使用AfterReadRecord事件来解决此问题。

[IgnoreEmptyLines()]
[ConditionalRecord(RecordCondition.ExcludeIfMatchRegex, "[0-9 A-Za-z.,]{1}S[0-9 A-Za-z.,]{10}")]
[FixedLengthRecord(FixedMode.ExactLength)]
public sealed class PurchaseOrder
{
[FieldFixedLength(1)]
[FieldTrim(TrimMode.Both)]
public string C;
[FieldFixedLength(1)]
[FieldTrim(TrimMode.Both)]
public string A;
[FieldFixedLength(10)]
[FieldTrim(TrimMode.Both)]
public string item;
}
internal class Program
{
static void Main(string[] args)
{
FileHelperEngine engine = new FileHelperEngine(typeof(PurchaseOrder));
// use the AfterReadRecord event instead of the INotifyRead interface
engine.AfterReadRecord += Engine_AfterReadRecord;
// The record will be skipped because of the Regex
var records = engine.ReadString("0S0123456789");
Debug.Assert(records.Length == 0);
Console.Write("All OK. No records were imported.");
Console.ReadKey();
}
// Define the event here instead of in your FileHelpers class
private static void Engine_AfterReadRecord(EngineBase engine, AfterReadRecordEventArgs e)
{
// not exist the property "SkipThisRecord"??
}

相关内容

  • 没有找到相关文章

最新更新