简体   繁体   English

使用“RecordCondition.ExcludeIfMatchRegex”

[英]Use of "RecordCondition.ExcludeIfMatchRegex"

Library version: v2.0.0.0库版本:v2.0.0.0

I would like to use ExcludeIfMatchRegex to exclude certain lines in the input file.我想使用ExcludeIfMatchRegex排除输入文件中的某些行。

I have tested next code but the system is displaying the usual message error Object reference not set to an instance of an object .我已经测试了下一个代码,但系统显示通常的消息错误Object reference not set to an instance of an object If I remove the line containing "ConditionalRecord", the system reads the file and returns the usual validation messages.如果我删除包含“ConditionalRecord”的行,系统会读取该文件并返回通常的验证消息。

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"??
    }
}

Looks like a small bug in the 2.0.0.0 library.看起来像 2.0.0.0 库中的一个小错误。

When the FileHelpers engine reads a file but ALL lines are excluded AND the class is decorated with INotifyRead it throws the Object Reference error.当 FileHelpers 引擎读取文件但排除所有行并且 class 装饰有 INotifyRead 时,它会引发Object 参考错误。

However you can work around it by using the AfterReadRecord event instead.但是,您可以改用 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"??
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM