Subclass of ClassMap<T> where Subclass has a constructor with a parameter
Hi,
Let's say I have subclassed a ClassMap that has a parameterized constructor with the names of the key columns that need to be combined into a composite key (as follows):
public class TestRecord
{
public string CompositeKey { get; set; }
}
public class MyClassMap : ClassMap<TestRecord>
{
public MyClassMap (params string[] keyColumns)
{
Map(x => x.CompositeKey).ConvertUsing(row => string.Join("_", keyColumns.Select(key => row.GetField(key))));
}
}
Invoked from Main:
void Main()
{
using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvReader(reader))
{
writer.WriteLine("key1,key2");
writer.Flush();
stream.Position = 0;
csv.Configuration.RegisterClassMap<MyClassMap>();
csv.GetRecords<TestRecord>().ToList().Dump();
}
}
Is there any way that I can pass in keyColumns for use in MyClassMap? Or is there a better approach? Thanks.
Try something like this:
var mappingObject = new MyClassMap(yourStringArray); csv.Configuration.RegisterClassMap(mappingObject);
Looks like this was possible at the time of the question, but now it's changed - you do
csv.Context.RegisterClassMap<MapperType>();
instead, and unfortunately there's no overload that accepts an instance. If MapperType has a constructor parameter, it throws an exception.
@jsabrooke That should still work on the latest version. There is an overload to pass in an instance.
@JoshClose sorry, yes you're right of course :-).
I hadn't removed the generic type parameter; was trying to do csv.Context.RegisterClassMap<MapperType>(instance);, whereas it should have been csv.Context.RegisterClassMap(instance);
Sorry!