CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Subclass of ClassMap<T> where Subclass has a constructor with a parameter

Open madeyegoody opened this issue 6 years ago • 4 comments

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.

madeyegoody avatar Feb 13 '19 23:02 madeyegoody

Try something like this: var mappingObject = new MyClassMap(yourStringArray); csv.Configuration.RegisterClassMap(mappingObject);

louisjrdev avatar Jul 04 '19 15:07 louisjrdev

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 avatar Jun 18 '21 17:06 jsabrooke

@jsabrooke That should still work on the latest version. There is an overload to pass in an instance.

JoshClose avatar Jun 24 '21 14:06 JoshClose

@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!

jsabrooke avatar Jun 24 '21 15:06 jsabrooke