using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using CsvHelper.Configuration;
using CsvHelper.Configuration.Attributes;
using CsvHelper.Expressions;
using CsvHelper.TypeConversion;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public interface ITestSaccaModel
public bool IsSelected { get; set; }
public bool IsModified { get; set; }
public bool IsDeleted { get; set; }
public Guid UIDOriginal { get; set; }
public string SomeValue { get; set; }
public class TestSaccaModelBaseClass : ITestSaccaModel
public bool IsSelected { get; set; }
public bool IsModified { get; set; }
public bool IsDeleted { get; set; }
public Guid UIDOriginal { get; set; }
public string SomeValue { get; set; } = "My Value";
public class TestSaccaModel : TestSaccaModelBaseClass;
public sealed class TestSaccaModelMap<TTestSaccaModel> : ClassMap<TTestSaccaModel>
where TTestSaccaModel : ITestSaccaModel
public TestSaccaModelMap()
AutoMap(CultureInfo.InvariantCulture);
Map(m => m.IsSelected).Ignore();
Map(m => m.IsModified).Ignore();
Map(m => m.IsDeleted).Ignore();
Map(m => m.UIDOriginal).Ignore();
static readonly string CsvDelimiter = ",";
static readonly string fileName = "Question79588082.csv";
public static (bool exists, string csvFileName) ExportDataCSV(List<ITestSaccaModel> data, string csvFileName)
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
Delimiter = CsvDelimiter,
using (var writer = File.CreateText(csvFileName))
using (var csv = new CsvWriter(writer, config))
csv.Context.RegisterClassMap<TestSaccaModelMap<ITestSaccaModel>>();
return (File.Exists(csvFileName), csvFileName);
public void Export_Data_To_CSV()
List<ITestSaccaModel> _dataCollection = new ()
new TestSaccaModel { SomeValue = "Value 1" },
new TestSaccaModel { SomeValue = "Value 2" }
var result = ExportDataCSV(_dataCollection.ToList(), fileName);
Console.WriteLine(File.ReadAllText(result.csvFileName));
IEnumerable<ITestSaccaModel> csvRecords;
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
using (var stream = File.OpenText(fileName))
using (var csv = new CsvReader(stream, config))
csv.Context.RegisterClassMap<TestSaccaModelMap<TestSaccaModel>>();
csvRecords = csv.GetRecords<TestSaccaModel>().ToList();
NUnit.Framework.Assert.That(csvRecords.Count() == _dataCollection.Count);
NUnit.Framework.Assert.That(System.Text.Json.JsonSerializer.Serialize(csvRecords) == System.Text.Json.JsonSerializer.Serialize(_dataCollection));
public static void Test()
new TestClass().Export_Data_To_CSV();
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}.", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("OS Version: {0}, NewLine: {1}", System.Environment.OSVersion, System.Text.Json.JsonSerializer.Serialize(Environment.NewLine));
Console.WriteLine("{0} version: {1}", typeof(CsvReader).Assembly.GetName().Name, typeof(CsvReader).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");