CsvHelper
CsvHelper copied to clipboard
Inheriting Parent index not working.
Describe the bug
From below, writing ExtraFieldsModel
to csv file, writes FullName
as 1st column and Email
as a 2nd column.
public class ExtraFieldsModel : BaseModel
{
public string FullName { get; set; }
}
public class BaseModel
{
[Index(0)]
public string Email { get; set; }
}
Expected behavior
Email
field to be the 1st column
Workaround
Add [Index(1)]
to the FullName
property
You need to specify the index on every property. The order can't be determined correctly without it.
I have found that only 2 property indexes need to be specified for Email
to be in 1st column. Any additional properties in "ExtraFIeldsModel" don't need index attributes, and they go in the natural order.
We really wish for the BaseModel Index to work without depending on the next Index(1) attribute. That way anyone creating new classes and inheriting BaseModel doesn't need to know about this attribute bug (I call it a bug just because Index1 is required. Index2, Index3... on other properties are not.)
Workaround:
public class ExtraFieldsModel : BaseModel
{
[Index(1)] //Just 1 attribute is enough to make [Index(0)] work.
public string FullName { get; set; }
public string Address {get;set;}
public DateTime DateOfBirth {get;set;}
}
public class BaseModel
{
[Index(0)]
public string Email { get; set; }
}
Why should the attributes on a base class take precedence over attributes on the child? I know that works well for you, but in general terms.
It's not a matter of taking precedence I don't think - I am only using 1 attribute. It's a matter of attribute working. The attribute should take precedence over all the other property column orders, right? That's why it's there, to over-write the order? I don't think it should matter how the final model gets generated, if it inherits something or not, it's just properties included in the child class in this case?