Using an array in a FileHelpers mapping class

I was looking for a way to allow one element of the FileHelpers mapping class to be an array of a specific length.

For example, I have a class like this:

[DelimitedRecord(",")] public class Example { public string code; public int month; public int day; public double h1; public double h2; public double h3; public double h4; } 

The values ​​h1-h4 would really make sense as an array simply called "h". This would facilitate file processing. I also know that the file that I am reading will always contain these and only these fields.

Has anyone figured out a way to include arrays in FileHelper mapping classes?

+4
source share
2 answers

FileHelpers record classes require public fields. A record class should not be considered a regular C # class, which should follow the best coding methods; rather, it's just the syntax for describing the structure of the import file.

The recommended procedure with FileHelpers is to loop through the resulting array Example[] and map the fields you need to a more normal class (with properties instead of public fields). At this point, you can copy your H1-H4 values ​​to an array property.

+1
source

I do not know anything about the instrument in question, but (assuming that this is not a limitation of the instrument), I really doubt the wisdom of public fields. Properties will also provide you with the ability to reinforce values:

 [DelimitedRecord(",")] public class Example { public string Code {get;set;} public int Month {get;set;} public int Day {get;set;} private readonly double[] h = new double[4]; public double H1 {get {return h[0];} set {h[0] = value;}} public double H2 {get {return h[1];} set {h[1] = value;}} public double H3 {get {return h[2];} set {h[2] = value;}} public double H4 {get {return h[3];} set {h[3] = value;}} } 

Again - I have no idea if this tool will help this, but it will be a viable way to implement it. Of course, the values ​​of "h" will do the same (in fact, a little more efficiently - without an array on the heap and without de-referencing) as direct members:

  public double H1 {get;set;} public double H2 {get;set;} public double H3 {get;set;} public double H4 {get;set;} 
0
source

All Articles