有没有办法做到使用FileHelpers库字段顺序? [英] Is there a way to do field order using the FileHelpers library?
问题描述
我从网上下载的NuGet FileHelpers但我不知道,如果这个功能不存在,或者,如果我没有正确的版本,或者是什么。
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.
我一直< A HREF =http://stackoverflow.com/questions/3975741/column-headers-in-csv-using-filehelpers-library>环视,似乎FileHelpers可能要指定字段属性订单。
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
推荐答案
首先,FieldOrder属性不FileHelpers 2.0存在。在FileHelpers 2.9.9(也可通过提供的NuGet),该属性存在,但如果你在任何领域,你必须将它指定为所有领域。在一般情况下,然而,使用该属性是没有必要的,因为该字段的顺序是由格式定义
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.
当使用FileHelpers你提供一类描述你的格式,例如:
When using FileHelpers you provide a class to describe your format, e.g.,
[DelimitedRecord("|")]
public class Order
{
// First field
public int OrderID;
// Second field
public string CustomerID;
// Third field
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime OrderDate;
}
这描述了三个字段的格式,用竖线分隔。如果你喜欢,它的是的格式的规范。一旦定义你可以用它来导入和导出:
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:
FileHelperEngine engine = new FileHelperEngine(typeof(Order));
// To read use:
Order[] orders = engine.ReadFile("FileIn.txt") as Order[];
// To write use:
engine.WriteFile("FileOut.txt", orders);
所以,如果你想以不同的顺序你的领域,你应该修改订单
类。
现在,如果你真的想,(用FileHelpers 2.9.9),你可以改变字段的顺序如下:
Now if you really wanted to, (with FileHelpers 2.9.9), you could change the order of the fields as follows:
[DelimitedRecord("|")]
public class Order
{
// Third field
[FieldOrder(3)]
public int OrderID;
// Second field
[FieldOrder(2)]
public string CustomerID;
// First field
[FieldOrder(1)]
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime OrderDate;
}
但它是清洁,以避免使用的 FieldOrder
属性,然后修改类,而不是内部的字段的顺序。
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 a string
Type orderType = ClassBuilder.ClassFromString(stringContainingOrderClassInCSharp);
FileHelperEngine engine = new FileHelperEngine(orderType);
Order[] orders = engine.ReadFile("FileIn.txt") as Order[];
或者你也可以使用 ClassBuilder
:
DelimitedClassBuilder cb = new DelimitedClassBuilder("Order");
// First field
cb.AddField("OrderID", typeof(int));
// Second field
cb.AddField("CustomerID", 8, typeof(string));
// Third field
cb.AddField("OrderDate", typeof(DateTime));
cb.LastField.Converter.Kind = ConverterKind.Date;
cb.LastField.Converter.Arg1 = "ddMMyyyy";
engine = new FileHelperEngine(cb.CreateRecordClass());
Order[] orders = engine.ReadFile("FileIn.txt") as Order[];
您可以使用您以添加字段必要为了喜欢的逻辑。
You can use whatever logic you like in order to add your fields in the necessary order.
这篇关于有没有办法做到使用FileHelpers库字段顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!