You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, we are porting legacy code to dotnet standard and are facing an issue which wasn't present in CsvHelper 2.5.0 net451.
The issue appears to be that CsvReader can't manage to resolve the correct ReturnType of the underlying properties in the mappings and is trying to map them all to System.Object
I have managed to reproduce it with a unit test in your solution:
[TestMethod]
public void CanCreateMapsFromDynamicList()
{
var data = new List<string[]>
{
new[] { "Col1", "Col2" },
new[] { "1", "one" },
new[] { "2", "two" }
};
var queue = new Queue<string[]>(data);
var parserMock = new ParserMock(queue);
var csvReader = new CsvReader(parserMock);
csvReader.Configuration.RegisterClassMap<SomeTypeClassMap>();
var records = csvReader.GetRecords<SomeType>().ToList();
Assert.IsNotNull(records);
Assert.AreEqual(2, records.Count);
}
public class SomeTypeClassMap : ClassMap<SomeType>
{
public SomeTypeClassMap()
{
Map(x => x.Id).Ignore();
var t = new SomeType();
foreach (var mapping in t.Mappings)
{
Map(mapping);
}
}
}
public class SomeType
{
public int Id { get; set; }
public string Col1 { get; set; }
public string Col2 { get; set; }
public IEnumerable<Expression<Func<SomeType, dynamic>>> Mappings =>
new List<Expression<Func<SomeType, dynamic>>> {i => i.Col1, i => i.Col2};
}
Error reported:
Test method CsvHelper.Tests.CsvReaderMappingTestsRuslan.CanCreateMapsFromDynamicList threw exception:
System.InvalidCastException: Unable to cast object of type 'CsvHelper.Configuration.MemberMap2[CsvHelper.Tests.CsvReaderMappingTestsRuslan+SomeType,System.String]' to type 'CsvHelper.Configuration.MemberMap2[CsvHelper.Tests.CsvReaderMappingTestsRuslan+SomeType,System.Object]'.
The text was updated successfully, but these errors were encountered:
Our use case is this: we import a bunch of different CSV documents, all of which have a set of common properties (columns) contained in a base-class and some individual defined in each respective entity class. This design allowed us to use a single ClassMap function for all of them before the upgrade. Do you think there might be a simple work-around?
Hi, we are porting legacy code to dotnet standard and are facing an issue which wasn't present in CsvHelper 2.5.0 net451.
The issue appears to be that CsvReader can't manage to resolve the correct
ReturnType
of the underlying properties in the mappings and is trying to map them all to System.ObjectI have managed to reproduce it with a unit test in your solution:
Error reported:
The text was updated successfully, but these errors were encountered: