Csvhelper: The best way to inject parameter into own ClassMap constructor definition

Created on 13 Jun 2017  路  6Comments  路  Source: JoshClose/CsvHelper

````c#
public sealed class MyClassMapWithParametrizationNeeded : CsvClassMap {
// I am not sure CsvHelper has construcor injection support for class maps
// What is the best way to make class maps parametrizable?
MyClassMapWithParametrizationNeeded (ClassMapDependencyParamter param) {

}
}

````

Most helpful comment

void Main()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvReader(reader))
    {
        writer.WriteLine("Id,Name");
        writer.WriteLine("1,one");
        writer.WriteLine(",");
        writer.WriteLine("2,two");
        writer.Flush();
        stream.Position = 0;

        var map = new TestMap(-1, "n/a");
        csv.Configuration.RegisterClassMap(map);
        csv.GetRecords<Test>().ToList().Dump();
    }
}

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public sealed class TestMap : ClassMap<Test>
{
    public TestMap(int id, string name)
    {
        Map(m => m.Id).Default(id);
        Map(m => m.Name).Default(name);
    }
}

All 6 comments

You should be able to do that. What is the issue?

hi Josh,
Hope this message finds you well, do you happen to have an example for this question.
I want to know how to tell csv helper to use the constructor of the map class with parameters defined.

Thanks
Lokesh Balu

void Main()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvReader(reader))
    {
        writer.WriteLine("Id,Name");
        writer.WriteLine("1,one");
        writer.WriteLine(",");
        writer.WriteLine("2,two");
        writer.Flush();
        stream.Position = 0;

        var map = new TestMap(-1, "n/a");
        csv.Configuration.RegisterClassMap(map);
        csv.GetRecords<Test>().ToList().Dump();
    }
}

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public sealed class TestMap : ClassMap<Test>
{
    public TestMap(int id, string name)
    {
        Map(m => m.Id).Default(id);
        Map(m => m.Name).Default(name);
    }
}

hi Josh,

Thank you for the quick reply, is there a way to write the constructor parameter to a row and not map it to any of the property and ignore it while reading the CSV file.

Thanks and Regards
Lokesh Balu

No, but you could manually do it I think. Add properties for the parameters and when writing, write those properties out on a commented line or something.

@JoshClose how to pass constructor params for the references mapping present in testmap

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dsotiriades picture dsotiriades  路  3Comments

muzzamo picture muzzamo  路  5Comments

Wagimo picture Wagimo  路  4Comments

KuraiAndras picture KuraiAndras  路  5Comments

RifS picture RifS  路  5Comments