FileHelpers 使用 MultiRecordEngine 异步?

FileHelpers async using MultiRecordEngine?

我目前正在使用 FileHelpers 库使用 MultiRecordEngine 从固定长度大小的文件中提取数据,方法与文档中的相同:

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

engine.RecordSelector = new RecordTypeSelector(CustomSelector);

var res = engine.ReadFile("Input.txt");

根据文档,FileHelperAsyncEngine 允许逐行异步读取文件。但是,这仅适用于一种指定类型的 T。

var engine = new FileHelperAsyncEngine<Customer>();

// Read
using(engine.BeginReadFile("Input.txt"))
{
    // The engine is IEnumerable
   foreach(Customer cust in engine)
   {
    // your code here
    Console.WriteLine(cust.Name);
   }
}

我无法找到任何 MultiRecordEngine 异步实现的示例,不确定这是否可能?任何建议或我应该将其保留为同步调用 engine.ReadFile()?

MultiRecordEngine 有支持异步使用的方法。请参阅文档 here

source code 的测试库中有几个示例。

[Test]
public void MultpleRecordsFileAsync()
{
    engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector),
        typeof (OrdersVerticalBar),
        typeof (CustomersSemiColon),
        typeof (SampleType));

    var res = new ArrayList();
    engine.BeginReadFile(FileTest.Good.MultiRecord1.Path);
    foreach (var o in engine)
        res.Add(o);

    Assert.AreEqual(12, res.Count);
    Assert.AreEqual(12, engine.TotalRecords);

    Assert.AreEqual(typeof (OrdersVerticalBar), res[0].GetType());
    Assert.AreEqual(typeof (OrdersVerticalBar), res[1].GetType());
    Assert.AreEqual(typeof (CustomersSemiColon), res[2].GetType());
    Assert.AreEqual(typeof (SampleType), res[5].GetType());
}

[Test]
public void MultpleRecordsWriteAsync()
{
    engine = new MultiRecordEngine(new RecordTypeSelector(CustomSelector),
        typeof (OrdersVerticalBar),
        typeof (CustomersSemiColon),
        typeof (SampleType));

    object[] records = engine.ReadFile(FileTest.Good.MultiRecord1.Path);

    engine.BeginWriteFile("tempoMulti.txt");
    foreach (var o in records)
        engine.WriteNext(o);
    engine.Close();
    File.Delete("tempoMulti.txt");


    object[] res = engine.ReadFile(FileTest.Good.MultiRecord1.Path);

    Assert.AreEqual(12, res.Length);
    Assert.AreEqual(12, engine.TotalRecords);

    Assert.AreEqual(typeof (OrdersVerticalBar), res[0].GetType());
    Assert.AreEqual(typeof (OrdersVerticalBar), res[1].GetType());
    Assert.AreEqual(typeof (CustomersSemiColon), res[2].GetType());
    Assert.AreEqual(typeof (SampleType), res[5].GetType());
}