CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Please update documentation regarding attributes.

Open JBastiaan opened this issue 4 years ago • 3 comments

When writing csv when you use a record like this:

public record Foo( [Name("Foobar")] string bar);

It will not write the csv using the Name attribute as expected. Reading does work. Please add this to the documentation, this will save people some headaches :).

JBastiaan avatar Dec 08 '21 16:12 JBastiaan

@JBastiaan Am I doing something wrong when using a record? I just tried the syntax listed above and it doesn't work, but it does work when I declare getters and setters.

// This one fails
public record Export([Name("id")] string Id, [Name("email")] string Email, [Name("date")] DateTime Date);

CsvHelper.HeaderValidationException: Header with name 'id'[0] was not found.
      Header with name 'email'[0] was not found.
      Header with name 'date'[0] was not found.

// This one works
public record Export
{
    [Name("id")]
    public string Id { get; set; } = default!;

    [Name("email")]
    public string Email { get; set; } = default!;

    [Name("date")]
    public DateTime Date { get; set; } = default!;
}

RKennedy9064 avatar Dec 13 '21 17:12 RKennedy9064

I ran into this, as well. a documentation fix would be cool; it'd be cooler if [Name] worked with records :P

BenMakesGames avatar Nov 16 '22 17:11 BenMakesGames

@BenMakesGames There is an issue for this. https://github.com/JoshClose/CsvHelper/issues/2048 It explains the issues regarding this.

There is a discussion that no one has commented on yet regarding it too. https://github.com/JoshClose/CsvHelper/discussions/2052

I suppose I'll just go and do whatever I want since no one has chimed in.

JoshClose avatar Nov 16 '22 21:11 JoshClose