Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c# - Is there a way to do field order using the FileHelpers library?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.,

[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); 

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:

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

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[]; 

Or you can use a 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...