使用MultiRecordEngine时无法使用AfterReadEvent

时间:2018-10-16 14:49:01

标签: c# filehelpers

我正在使用MultiRecordEngine作为分隔符('|')输入文件。因为我需要检查/验证某些字段不超过某些大小,并且某些字段必须包含“ YES”或“ NO”值。但是,即使我遵循filehelper网站上的示例,也无法加入AfterReadEvent

1 个答案:

答案 0 :(得分:0)

使用the MultiRecordEngine example here中的类定义,以下示例演示了有效的AfterReadEvent

public class Program
{
    private static int _EventCount = 0;
    public static void Test_Import()
    {
        var str = @"10248|VINET|5|04071996|01081996|16071996|3|32.38" + Environment.NewLine
                    + "10249|TOMSP|6|05071996|16081996|10071996|1|11.61" + Environment.NewLine
                    + "ALFKI;Alfreds Futterkiste;Maria Anders;Sales Representative;Obere Str. 57;Berlin;Germany" + Environment.NewLine
                    + "ANATR;Ana Trujillo Emparedados y helados;Ana Trujillo;Owner;Avda. de la Constitución 2222;México D.F.;Mexico" + Environment.NewLine
                    + "10250 |HANAR|4|08071996|05081996|12071996|2|65.83" + Environment.NewLine
                    + "10111314012345";

        var engine = new MultiRecordEngine(typeof(Orders),
            typeof(Customer),
            typeof(SampleType));

        engine.RecordSelector = new RecordTypeSelector(CustomSelector);

        engine.AfterReadRecord += Engine_AfterReadRecord;

        var records = engine.ReadString(str);
        Assert.AreEqual(6, records.Count());
        Assert.AreEqual(6, _EventCount);
    }

    private static void Engine_AfterReadRecord(EngineBase engine, AfterReadEventArgs<object> e)
    {
        // Increment the event counter
        _EventCount++;
    }

    public static void Main(string[] args)
    {
        Test_Import();
        Console.WriteLine("OK");
        Console.ReadKey();
    }
}