Search code examples
c#filehelpers

Adding an Array Field using FileHelpers DelimitedClassBuilder


How do I do this dynamically using DelimitedClassBuilder so that the columns in the file can expand but not break my program?

[DelimitedRecord(",")]
public class MyRecord
{
    public string Name;

    [FieldOptional, FieldArrayLength(0, 100)]
    public string[] I_DONT_CARE_WHAT_COMES_AFTER_THIS;
}

i.e. how do I finish this:

var cb = new DelimitedClassBuilder("xyz", ",");
cb.AddField("Name", "string");
... how do I add the array field here?
Type type = cb.CreateRecordClass();
var engine = new DelimitedFileEngine(type);

Solution

  • Good question. The best I can find is:

    var cb = new DelimitedClassBuilder("xyz", ",");
    cb.AddField("Name", "string");
    cb.AddFields(100);
    foreach (var field in cb.Fields.Where(f => f.FieldName.StartsWith("Field")))
    {
        field.FieldOptional = true;             
    }
    var type = cb.CreateRecordClass();
    var engine = new DelimitedFileEngine(type);
    

    I can't get it to work with

    cb.AddField("I_DONT_CARE", typeof(string[]));
    

    Nor with

    cb.AddField("I_DONT_CARE", typeof(string[]).FullName);
    

    both of which ought to work.