Ignoring Properties in FileHelpers

I use FileHelpers to export models to CSV. It has an attribute [FieldNotInFile ()], which excludes fields when exporting, but I need to use the properties as I need for some other attributes, as well as from another third-party library that works only with properties.

Is there a way to make FileHelpers ignore a property?

+6
source share
4 answers

I had the same problem the other day, and I used the [FieldHidden] attribute. Something like that:

 [DelimitedRecord("\t")] public class PolicyFileRecord { public string FileDate; public int ProgramId; public string LocationAddress1; public string LocationAddress2; public string LocationAddress3; public string LocationCity; public string LocationState; public string LocationZip; [FieldHidden] public string LocationCountry; } 
+4
source

Starting with release3.27, you can use [FieldHidden] in AND properties fields.

+1
source

The FileHelpers class is just a way to define a flat file specification using the restricted C # syntax as the definition language. Thus, FileHelpers classes are an unusual type of C # class, and you should not try to use them in any other way. Imagine that the FileHelpers class is a โ€œspecificationโ€ of your CSV format. This should be his only role. If you need entries in a more โ€œnormalโ€ object (in your case, you need properties instead of fields), then compare the results with something like this:

 FileHelperEngine engine = new FileHelperEngine<FileHelpersOrder>(); var records = engine.ReadFile("FileIn.txt"); var niceOrders = records.Select( x => new NiceOrder() { Number = x.Number, Customer = x.Customer // etc. }); 

Where FileHelpersOrder is your CSV specification and NiceOrder will be a suitable OOP class with properties, methods, etc. as needed.

If you export, you need to do the opposite, i.e. Select the FileHelpersOrder collection from the NiceOrder collection.

0
source

I got this to work by providing a support field property and marking the support field as [FieldHidden] :

 [DelimitedRecord(",")] public class Record { public int Id; public string Name; public string SomeProperty { get { return someProperty; } set { someProperty = value; } } [FieldHidden] private string someProperty; } 
0
source

Source: https://habr.com/ru/post/981562/


All Articles