I'm using the C# FileHelpers library to efficiently parse large delimited files into validation objects.
However, I'd like to be able to map a single input file column to more than one output class property, but can't find an obvious way to achieve this. I've looked at ITransformable, but I don't want to map to yet another object to cut down on memory during the operation, and I've looked at the DynamicFieldBuilder/DynamicClassBuilder objects, but these seem to only allow me to describe what's in the input file, not what should be in the output instance.
I am trying to avoid having to load the file twice or do some kind of object-to-object mapping after the fact.
Input File Example:
ColumnA|ColumnB
Foo|Baz
Output Class Example:
public class FooBar
{
public string ColumnA_One;
public string ColumnA_Two;
public string ColumnB_One;
public string ColumnB_Two;
}
You can mark the duplicate columns with the FieldIgnored
attribute and use the AfterReadRecord
event to fill them.
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;
}
}