如何让 FileHelpers 忽略定义列之后的列? (即忽略末尾的列)
How to make FileHelpers ignore columns after the defined columns? (i.e. ignore columns at the end)
假设我有以下 FileHelpers 记录定义:
[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class TestRecord
{
[FieldCaption("A")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string A;
[FieldCaption("B")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string B;
[FieldCaption("C")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string C;
}
我只对 A、B、C 列感兴趣。它们后面的任何列都可以忽略。因此,例如,我希望能够像这样处理数据:
A,B,C,D,E
TestA1,TestB1,TestC1,TestD1,TestE1
TestA2,TestB1,TestC1,TestD1,TestE1
或:
A,B,C,D
TestA1,TestB1,TestC1,TestD1
TestA2,TestB1,TestC1,TestD1
或:
A,B,C
TestA1,TestB1,TestC1
TestA2,TestB1,TestC1
只需在 class 的末尾添加一个额外的 string[]
字段并将其标记为 [FieldOptional]
。
[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class TestRecord
{
[FieldCaption("A")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string A;
[FieldCaption("B")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string B;
[FieldCaption("C")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string C;
[FieldOptional]
public string[] EverythingElse;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<TestRecord>();
var records = engine.ReadString("TestA1,TestB1,TestC1,TestD1,TestE1");
Debug.Assert(records.Count() == 1);
Debug.Assert(records[0].A == "TestA1");
Debug.Assert(records[0].B == "TestB1");
Debug.Assert(records[0].C == "TestC1");
Console.WriteLine("All OK");
Console.ReadLine();
}
}
假设我有以下 FileHelpers 记录定义:
[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class TestRecord
{
[FieldCaption("A")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string A;
[FieldCaption("B")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string B;
[FieldCaption("C")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string C;
}
我只对 A、B、C 列感兴趣。它们后面的任何列都可以忽略。因此,例如,我希望能够像这样处理数据:
A,B,C,D,E
TestA1,TestB1,TestC1,TestD1,TestE1
TestA2,TestB1,TestC1,TestD1,TestE1
或:
A,B,C,D
TestA1,TestB1,TestC1,TestD1
TestA2,TestB1,TestC1,TestD1
或:
A,B,C
TestA1,TestB1,TestC1
TestA2,TestB1,TestC1
只需在 class 的末尾添加一个额外的 string[]
字段并将其标记为 [FieldOptional]
。
[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class TestRecord
{
[FieldCaption("A")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string A;
[FieldCaption("B")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string B;
[FieldCaption("C")]
[FieldQuoted(QuoteMode.OptionalForBoth)]
public string C;
[FieldOptional]
public string[] EverythingElse;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<TestRecord>();
var records = engine.ReadString("TestA1,TestB1,TestC1,TestD1,TestE1");
Debug.Assert(records.Count() == 1);
Debug.Assert(records[0].A == "TestA1");
Debug.Assert(records[0].B == "TestB1");
Debug.Assert(records[0].C == "TestC1");
Console.WriteLine("All OK");
Console.ReadLine();
}
}