v2.0.0 版本中"AfterRead"事件



我的目标是在读取字段后更改字段的值。我认为运行' afterread '是可能的;事件。这个库的当前版本是2.0.0。

我已经测试了下一个得到一个错误:

public class sealed MyClass: INotifyRead
{
// ...
public void AfterRead(EngineBase engine, AfterReadEventArgs e)
{
}
}

错误信息是:

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

我已经阅读了文档中的下一个代码:

FileHelperEngine engine = new FileHelperEngine(typeof(Orders));
engine.BeforeReadRecord += new BeforeReadRecordHandler(BeforeEvent);
Orders[] res = engine.ReadFile("report.txt") as Orders[];

我不知道是否需要在源代码中声明委托,或者在映射类中声明事件就足够了。

非常感谢!

2.0.0.0中AfterRead事件的签名不同。下面是一个工作示例:

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();
}
}
}

最新更新