Reputation: 7346
I am using JoshClose's CsvHelper library in order to serialize and deserialize data into CSV.
I am facing the following problem : When deserializing my data, I want to use a specific function to create a new instance of my class being deserialized.
I saw I can use a specific constructor as indicated here, but in my case, I would like to use a function which is not a constructor (but still returns a new instance of my class). Is this even possible?
Another solution would be to allow CSVHelper to "override" the values of an existing instance of my class. Is this possible?
Here is my current code so far:
private MyClass[] DeserializeFromCSV( string csv )
{
MyClass[] output = null;
using ( System.IO.TextReader textReader = new System.IO.StringReader( csv ) )
using ( CsvHelper.CsvReader csvReader = new CsvHelper.CsvReader( textReader ) )
{
// Here, I would like to provide the function to call
// in order to instantiate a new instance of the class
// The function I want to call does not return a `ConstructorInfo`
// as needed by the property here
//csvReader.Configuration.GetConstructor = type =>
//{
// return null;
//};
// CSVClassMap is a getter to get a ClassMap
csvReader.Configuration.RegisterClassMap( CSVClassMap );
csvReader.Read();
output = csvReader.GetRecords<T>().ToArray();
}
return output;
}
Upvotes: 0
Views: 635
Reputation: 23383
You can use CsvHelper.ObjectResolver
to do this.
void Main()
{
var s = new StringBuilder();
s.Append("Id,Name\r\n");
s.Append("1,one\r\n");
s.Append("2,two\r\n");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader))
{
CsvHelper.ObjectResolver.Current = new ObjectResolver(CanResolve, Resolve);
csv.Configuration.RegisterClassMap<TestMap>();
csv.GetRecords<Test>().ToList().Dump();
}
}
public bool CanResolve(Type type)
{
return type == typeof(Test);
}
public object Resolve(Type type, object[] constructorArgs)
{
// Get a dependency from somewhere.
var someDependency = new object();
return new Test(someDependency);
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(object someDependency) { }
}
public class TestMap : ClassMap<Test>
{
public TestMap()
{
Map(m => m.Id);
Map(m => m.Name);
}
}
Upvotes: 2