Issue: Custom converters are not triggered during CSV file writing with CsvHelper

36 views Asked by At

I'm facing an issue while writing a CSV file using CsvHelper in C#. I have defined some custom converters to handle the conversion of certain data types during writing, however, it seems that these converters are not triggered during the code execution.

Here's my code:

// Include necessary namespaces...

public static class CsvConverter
{
    public static void ListinoBaseToCsv(List<RigaListinoArticoliDb> listino, string filePath)
    {
        try
        {
            using var writer = new StreamWriter(filePath);
            using var csv = new CsvWriter(writer, new 
        CsvConfiguration(CultureInfo.InvariantCulture));
            csv.Context.RegisterClassMap<RigaListinoArticoliDbMap>();
            csv.WriteRecords(listino);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred while writing the CSV file: {ex.Message}");
            throw;
        }
    } 
}

// Definition of custom converters and mapping of classes...

// Call to CsvConverter.ListinoBaseToCsv(listino, filePath);

Despite having defined custom converters for data types such as double and List, it seems that they are not being used during the CSV file writing. I have carefully checked the code and can't seem to find why the converters are not triggered.

Has anyone had similar experiences or any ideas on what I could check or modify to ensure that the converters are properly used during the CSV file writing?

Thanks in advance for any suggestions or assistance.

Best regards, Simone

0

There are 0 answers