我正在使用C#FileHelpers库来有效地将大分隔文件解析为验证对象。
但是,我希望能够将单个输入文件列映射到多个输出类属性,但无法找到实现此目的的明显方法。我看过ITransformable,但是我不想在操作期间映射到另一个对象以减少内存,我已经查看了DynamicFieldBuilder / DynamicClassBuilder对象,但这些似乎只允许我描述什么是在输入文件中,而不是输出实例中的内容。
我试图避免在事后加载文件两次或进行某种对象到对象的映射。
输入文件示例:
ColumnA|ColumnB
Foo|Baz
输出类示例:
public class FooBar
{
public string ColumnA_One;
public string ColumnA_Two;
public string ColumnB_One;
public string ColumnB_Two;
}
答案 0 :(得分:1)
您可以使用FieldIgnored
attribute标记重复的列,然后使用AfterReadRecord
event填充它们。
class Program
{
[DelimitedRecord("|")]
public class FooBar
{
public string ColumnA_One;
[FieldIgnored]
public string ColumnA_Two;
public string ColumnB_One;
[FieldIgnored]
public string ColumnB_Two;
}
static void Main(string[] args)
{
FileHelperEngine engine = new FileHelperEngine(typeof(FooBar));
engine.AfterReadRecord += engine_AfterReadRecord;
FooBar[] records = engine.ReadFile("FileIn.txt") as FooBar[];
}
static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<object> e)
{
FooBar fooBar = e.Record as FooBar;
fooBar.ColumnA_Two = fooBar.ColumnA_One;
fooBar.ColumnB_Two = fooBar.ColumnB_One;
}
}
答案 1 :(得分:0)
enum DataFields
{
Action = 0,
CRC = 1,
Desc = 2,
Group = 3,
Name = 4,
Sell = 5,
EffDate = 6,
Whse = 7,
Whse2 = 8,
Whse3 = 9,
Casepack = 10,
LDU = 11,
Cube = 12,
Item = 13,
UPC = 14,
Cost = 15,
Markup = 16,
OldSell = 17,
Avail = 18,
Substitute = 19,
Poll = 20
}//enum DataFields
/// <summary>
/// This will hold the ordinal position of the NameFields within the datafile
/// </summary>
enum NameFields
{
Code = 0,
Abrv = 1,
Name = 2,
Count = 3
}//enum NameFields
/// <summary>
/// This will hold the ordinal position of the values when populating the history table
/// </summary>
enum HistoryFields
{
CRC = 0,
EffDate = 1,
OldSell = 2,
Sell = 3
}//enum HistoryFields
#endregion