"AfterRead" event in the v2.0.0 version

36 views Asked by At

My goal is to change the value of a field after it is read. And I think it is possible running the "AfterRead" event. My current version of the library is 2.0.0.

I have tested the next getting an error:

public class sealed MyClass: INotifyRead
{
  // ...

  public void AfterRead(EngineBase engine, AfterReadEventArgs e)
  {
  }
}

The error message is:

Cannot find the namespace "AfterReadEventArgs". Missing the "using" directive or assembly reference.

I have read the next code in the docs:

FileHelperEngine engine = new FileHelperEngine(typeof(Orders));
engine.BeforeReadRecord += new BeforeReadRecordHandler(BeforeEvent);

Orders[] res = engine.ReadFile("report.txt") as Orders[];

I don't know if it is needed to declare the delegate in the source code or it is enough the declaration of the event in the Mapping Class.

Thanks a lot!

1

There are 1 answers

0
shamp00 On BEST ANSWER

The signature of the AfterRead event is different in 2.0.0.0. Here's a working example.

using System;
using System.Diagnostics;
using FileHelpers;

namespace ConsoleApp
{
    [DelimitedRecord(";")]
    public sealed class MyClass : INotifyRead
    {
        public string Field1;
        public string Field2;

        public void AfterRead(EngineBase engine, string line)
        {
            Field1 = "ModifiedValue1";
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(MyClass));
            var records = engine.ReadString("Value1;Value2");
            var firstRecord = records[0] as MyClass;

            Debug.Assert(firstRecord.Field1 == "ModifiedValue1");

            Console.Write("All OK. Field1 had the modified value.");
            Console.ReadKey();
        }
    }
}