Ale*_*der 6 filehelpers c#-4.0
有没有机会让MultiRecordEngine为每条记录设置行号?我阅读了文档,发现Engine有一个LineNumber属性,但我找不到使用它的方法.
我想拥有什么:即:
[FixedLengthRecord]
public class Employee
{
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;
}
Run Code Online (Sandbox Code Playgroud)
或者......我可以依赖Engine.ReadFile创建的集合中的记录索引吗?即:
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?
}
Run Code Online (Sandbox Code Playgroud)
您可以使用该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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1454 次 |
| 最近记录: |