FileHelper 无法在其 类 中处理 ReSharper 排序变量

FileHelper can't handle ReSharper sorting variables in its classes

我有这样做的代码:

var engine = new FileHelperEngine<SampleType>();
var records = engine.ReadString("asdf|fdsa");

var showString = records[0].Field1 + records[0].Field2;

SampleType class 看起来像这样:

[DelimitedRecord("|")]
public class SampleType
{
    public string Field2;
    public string Field1;
}

结果如下:

records[0].Field1 的值为 fdsa

records[0].Field2 的值为 asdf

如果我 运行 进行 ReSharper 清理,它将按字母顺序对变量进行排序。 SampleType class 将如下所示:

[DelimitedRecord("|")]
public class SampleType
{
    public string Field1;
    public string Field2;
}

但是现在我的程序逻辑变了。

records[0].Field1 的值为 asdf

records[0].Field2 的值为 fdsa

有没有办法告诉 classes 定义变量的顺序无关紧要?变量的定义顺序突然变得相关,这与我所见过的任何其他 class 相反,我觉得非常令人不安和奇怪。

我不完全确定,但我 认为 您想要一种方法让 FileHelpers 使用字段的显式指定顺序,而不是隐式的 'the order they are defined in in the source'.

如果我是对的,您需要 FileHelpers 库中的 the FieldOrder attribute

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;
}

关于你的评论

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

你是对的,这里 FileHelper 的默认操作模式(使用字段的源顺序作为它们的记录顺序)不是 'native' C#,而且确实不是特别是 'C#-ish' 做事方式;但是如果 FileHelpers 的作者和用户想要它,那也没关系。它确实具有更动态语言风格的风格,但这就是 一点 C# 的发展方向...