[英]How to make FileHelpers ignore columns after the defined columns? (i.e. ignore columns at the end)
Suppose I have the following FileHelpers Record definition:假设我有以下 FileHelpers Record 定义:
[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;
}
I am only interested in columns A,B,C.我只对 A、B、C 列感兴趣。 Any columns that follow them can be ignored.可以忽略它们后面的任何列。 So for example, I would like it to be possible to handle data like this:例如,我希望能够处理这样的数据:
A,B,C,D,E
TestA1,TestB1,TestC1,TestD1,TestE1
TestA2,TestB1,TestC1,TestD1,TestE1
or:或者:
A,B,C,D
TestA1,TestB1,TestC1,TestD1
TestA2,TestB1,TestC1,TestD1
or:或者:
A,B,C
TestA1,TestB1,TestC1
TestA2,TestB1,TestC1
Just add an extra string[]
field to the end of your class and mark it [FieldOptional]
.只需在 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.