Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring properties in FileHelpers

I am using FileHelpers for exporting models to CSV. It has a [FieldNotInFile()] attribute which excludes fields when exporting, but I need to use properties as I need to some other attributes as well from another 3rd party library which only work with properties.

Is there any way to get FileHelpers to ignore a property?

like image 957
mclaassen Avatar asked Jan 26 '15 16:01

mclaassen


2 Answers

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

[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;
}
like image 180
Red Avatar answered Sep 21 '22 22:09

Red


I got this to work by giving the property a backing field and marking the backing 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;
}
like image 32
Sam Avatar answered Sep 25 '22 22:09

Sam