Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何让FileHelper在每条记录上设置行号?_C# 4.0_Filehelpers - Fatal编程技术网

C# 4.0 如何让FileHelper在每条记录上设置行号?

C# 4.0 如何让FileHelper在每条记录上设置行号?,c#-4.0,filehelpers,C# 4.0,Filehelpers,是否有机会让MultiRecordEngine为每条记录设置行号? 我阅读了文档,发现引擎有一个LineNumber属性,但找不到使用它的方法 我想要的是: 即。: 您可以使用AfterReadRecord事件设置LineNumber属性 下面是一个工作示例 public class Employee { [FieldIgnored] public int LineNumber; // <-- setup by the engine while reading the f

是否有机会让MultiRecordEngine为每条记录设置行号? 我阅读了文档,发现引擎有一个LineNumber属性,但找不到使用它的方法

我想要的是: 即。:


您可以使用
AfterReadRecord
事件设置LineNumber属性

下面是一个工作示例

public class Employee
{
    [FieldIgnored]
    public int LineNumber; // <-- setup by the engine while reading the file

    [FieldFixedLength(1)]
    public string Type = "2";

    [FieldFixedLength(6)]
    [FieldTrim(TrimMode.Left, "0")]
    public int ID;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Employee>();
        engine.AfterReadRecord += engine_AfterReadRecord;
        Employee[] records = engine.ReadString("2000003" + Environment.NewLine 
                                             + "5000006");

        Employee firstRecord = records[0];
        Assert.AreEqual(1, firstRecord.LineNumber);
        Assert.AreEqual("2", records[0].Type);
        Assert.AreEqual(3, records[0].ID);

        Employee secondRecord = records[1];
        Assert.AreEqual(2, secondRecord.LineNumber);
        Assert.AreEqual("5", records[1].Type);
        Assert.AreEqual(6, records[1].ID);

        Console.Read();
    }

    static void engine_AfterReadRecord(EngineBase engine, AfterReadEventArgs<Employee> e)
    {
        e.Record.LineNumber = engine.LineNumber;
    } 
}
公共类员工
{
[字段忽略]

public int LineNumber;//或者,您也可以让您的记录类实现
INotifyRead
,而不是使用
AfterReadRecord
事件。然后所有代码都在FileHelpers类中。谢谢shamp00!您的替代方案更好!谢谢,INotifyRead正是我要搜索的:)。顺便说一句,FieldIgnored已经被忽略了me“FieldHidden”![FieldIgnored]现在被禁用,请改用[FieldHidden]。
  static void Main(string[] args)
    {

        var engine = new MultiRecordEngine(CustomSelector, _types);
        var obj = engine.ReadFile("order.txt");

        // obj.GetValue(100) returns same record found on the 101th line in the file?
    }
public class Employee
{
    [FieldIgnored]
    public int LineNumber; // <-- setup by the engine while reading the file

    [FieldFixedLength(1)]
    public string Type = "2";

    [FieldFixedLength(6)]
    [FieldTrim(TrimMode.Left, "0")]
    public int ID;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Employee>();
        engine.AfterReadRecord += engine_AfterReadRecord;
        Employee[] records = engine.ReadString("2000003" + Environment.NewLine 
                                             + "5000006");

        Employee firstRecord = records[0];
        Assert.AreEqual(1, firstRecord.LineNumber);
        Assert.AreEqual("2", records[0].Type);
        Assert.AreEqual(3, records[0].ID);

        Employee secondRecord = records[1];
        Assert.AreEqual(2, secondRecord.LineNumber);
        Assert.AreEqual("5", records[1].Type);
        Assert.AreEqual(6, records[1].ID);

        Console.Read();
    }

    static void engine_AfterReadRecord(EngineBase engine, AfterReadEventArgs<Employee> e)
    {
        e.Record.LineNumber = engine.LineNumber;
    } 
}