c# - 如何使 FileHelpers 忽略定义列之后的列?(即忽略最后的列)
问题描述
假设我有以下 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;
}
我只对 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
解决方案
只需在类的末尾添加一个额外的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();
}
}
推荐阅读
- reactjs - ReactJS - 超出最大更新深度错误
- three.js - Three.js NPM 相机模糊如果添加后处理效果
- python - python将char与int相乘为什么结果是44?
- pandas - DataFrame append() 方法不追加数据
- html - 如何删除网页顶部不必要的空格?
- c# - 实体框架在保存到数据库时将可空的 unsigned int 字段的值设为空
- python - 一个支持加法和否定的模整数的python类?
- node.js - 无法建立连接NodeJS
- terraform - Terraform Cloud(即远程后端)TF_VAR_ 环境替换不起作用?
- ruby-on-rails - 多态关联和has_many:通过在尝试获取通过关联记录时生成错误的sql,rails?