I have the following code:
public static void WriteRecords(string fileSpecification, string[] headerRow, Type classMapType, int[] indexes, IEnumerable<object> records)
{
using (StreamWriter streamWriter = new StreamWriter(fileSpecification))
using (CsvWriter csvWriter = new CsvWriter(streamWriter, noHeaderConfiguration))
{
csvWriter.Context.RegisterClassMap(classMapType); // How do I pass in the indexes without hardcoding
WriteHeader(headerRow, csvWriter);
csvWriter.NextRecord();
csvWriter.WriteRecords(records);
}
}
// Edit One of the maps, does not mean only one I am calling.
public sealed class TimeZone_StartDate_Value_isGood_Description_FormatModelMap : ClassMap<TimeZone_StartDate_Value_isGood_Description_FormatModel>
{
public TimeZone_StartDate_Value_isGood_Description_FormatModelMap(int[] indexes)
{
Map(m => m.TimeZone).Index(indexes[0]);
Map(m => m.StartDate).Index(indexes[1]);
Map(m => m.Value).Index(indexes[2]);
Map(m => m.IsGoodString).Index(indexes[3]);
Map(m => m.Description).Index(indexes[4]);
}
}
// Edit 2 WriteHeader Method, asked to be included. Write header row for csv
private static void WriteHeader(string[] headerRow, CsvWriter csvWriter)
{
for (int i = 0; i < headerRow.Length; i++)
{
csvWriter.WriteField(headerRow[i]);
}
}
I want to pass in int[] indexes to a map. The map is determined by parameter Type classMapType. The class map( not included here) has a constructor that takes in int[]. How does one achieve this?
I have tried Activator.CreateInstance() but that returns a object.
Thank you for your time.
EDIT: I included one of the class maps, does not mean it is the only one called.
I tried to use .GetType() but returns error because there is no ctor with zero parameters. Tried to find another method but could not.
csvWriter.Context.RegisterClassMap(new MyClassMap(indexes));