我从 nuget 下载了 FileHelpers,但我不确定此功能是否不存在或者我没有正确的版本或什么。
我环顾四周,似乎 FileHelpers 可能有一个属性指定字段顺序。
我下载了 这个 但是当我在 nuget 中查找时似乎有 另一个版本
I downloaded FileHelpers from nuget but I am not sure if this feature does not exist or if I don't have the right version or what.
I been looking around and it seems that FileHelpers may have an attribute to specify the field order.
I downloaded this one however when I was looking in nuget there seems to be another version
发布评论
评论(1)
首先,FileHelpers 2.0 中不存在 FieldOrder 属性。在 FileHelpers 2.9.9(也可通过 NuGet 获得)中,该属性存在,但如果为任何字段指定它,则必须为所有字段指定它。然而,一般来说,没有必要使用该属性,因为字段的顺序是由格式定义的。
使用 FileHelpers 时,您提供一个类来描述您的格式,例如,
这描述了具有三个字段的格式,由竖线分隔。如果您愿意的话,它是格式的规范。定义后,您可以使用它来导入和导出:
因此,如果您希望字段采用不同的顺序,则应该修改您的
Order
类。现在,如果您确实愿意(使用 FileHelpers 2.9.9),您可以按如下方式更改字段的顺序:
但避免使用
FieldOrder
属性并修改字段的顺序会更干净。而是类中的字段。另一方面,如果您需要在运行时指定字段顺序,则应使用 Order 类rel="noreferrer">运行时记录。您可以使用
string
或者您可以使用
ClassBuilder
:您可以使用您喜欢的任何逻辑,以便按必要的顺序添加字段。
Firstly, the FieldOrder attribute does not exist in FileHelpers 2.0. In FileHelpers 2.9.9 (also available via NuGet), the attribute exists but if you specify it for any field, you must specify it for all fields. In general, however, use of the attribute is no necessary, since the order of the fields is defined by the format.
When using FileHelpers you provide a class to describe your format, e.g.,
This describes a format with three fields, separated by vertical bars. If you like, it is the specification of the format. Once defined you can use it to import and export:
So, if you want your fields in a different order, you should modify your
Order
class.Now if you really wanted to, (with FileHelpers 2.9.9), you could change the order of the fields as follows:
but it is cleaner to avoid the use of the
FieldOrder
attribute and modify the order of the fields within the class instead.On the other hand, if you need to specify the field order at runtime, you should build the
Order
class at using runtime records. You can use astring
Or you can use a
ClassBuilder
:You can use whatever logic you like in order to add your fields in the necessary order.