I have code doing this:
var engine = new FileHelperEngine<SampleType>();
var records = engine.ReadString("asdf|fdsa");
var showString = records[0].Field1 + records[0].Field2;
The SampleType class looks like this:
[DelimitedRecord("|")]
public class SampleType
{
public string Field2;
public string Field1;
}
The resultset is as follows:
records[0].Field1
has the value fdsa
records[0].Field2
has the value asdf
If I run a ReSharper cleanup it will sort the variables alphabetically. And the SampleType class will look like this:
[DelimitedRecord("|")]
public class SampleType
{
public string Field1;
public string Field2;
}
But now the logic of my program has changed.
records[0].Field1
has the value asdf
records[0].Field2
has the value fdsa
Is there a way to tell the classes that the order of the defined variables is irrelevant? That the defining order of variables is suddenly relevant, contrary to any other class I have ever seen, I find very disturbing and strange.
I'm not completely sure, but I think you want a way to make FileHelpers use an explicitly-specified ordering for the fields, rather than the implicit 'the order they are defined in in the source'.
If I'm right, you want the FieldOrder
attribute from the FileHelpers library:
Force field order with [FieldOrder] attribute:
//-> You first declare a Record Mapping class: Input.txt 10248|VINET|04071996|32.38 10249|TOMSP|05071996|11.61 10250|HANAS|08071996|65.83 10251|VICTE|08071996|41.34 RecordClass.cs [DelimitedRecord("|")] public class Orders { [FieldOrder(20)] public string CustomerID; [FieldConverter(ConverterKind.Date, "ddMMyyyy")] [FieldOrder(30)] public DateTime OrderDate; [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is "." [FieldOrder(40)] public decimal Freight; [FieldOrder(10)] public int OrderID; }
As to your comment
However I am quite surprised of how OK this way of solving a problem in such a library seems to you. And for the record, this is not logical, and to be able to understand it, you'll have to "overthink" :p It's not a part of the languages design
You're right in that the default operation mode of FileHelper here (to use the source-ordering of fields as their in-record ordering) isn't 'native' C#, and indeed isn't a particularly 'C#-ish' way of doing things; BUT that's OK if it's how the authors and users of FileHelpers want it. It does have the flavour of a more dynamic-language style, but then that's a bit how C# is headed anyway...