CSVHelper doesn't store HashSet fields

55 Views Asked by At

I'm working on a software project where the program has a data storage and a UI that lets you manipulate this data. I use multiple data types, some of which contain HashSets. I don't do anything complex, it's just HashSet<int> in each case, but when I try to save a list of these records, only the primitives and enums get written to the csv file, the hashsets are completely ignored.

Here's an example of what I'm trying to do:

using CsvHelper;
using CsvHelper.Configuration.Attributes;

internal class Program {

    private static readonly List<Record> records = new();

    private static void Main(string[] args) {
        Console.WriteLine("Hello, World!");

        records.Add(new Record() { Id = 1, Values = new() { 1, 2, 3, 4 } });
        records.Add(new Record() { Id = 2, Values = new() { 5, 6, 7, 8 } });
        records.Add(new Record() { Id = 3, Values = new() { 9, 10, 11, 12 } });

        using StreamWriter sw = new("records.csv");
        using CsvWriter csv = new(sw, System.Globalization.CultureInfo.InvariantCulture);
        csv.WriteRecords(records);
    }

}

public class Record {
    [Name("id")]
    public int Id { get; set; }
    [Name("values")]
    public HashSet<int> Values { get; set; }
}

This is what I expect the csv file to look like (curly braces are optional):

CSV data populated

This is what I actually get:

CSV data missing

0

There are 0 best solutions below