getting no properties are mapped for type using csvHelper
The accepted answer is working correctly, but there is another way to solve this problem as the following
CsvConfiguration configuration = new CsvConfiguration
{
IgnorePrivateAccessor = true,
}
CsvReader reader = new CsvReader(/*your TextReader*/, configuration);
Documentation
Gets or sets a value indicating if a private member should be read from and written to. True to include private members, otherwise false. Default is false.
NOTE You could face the same problem even if you have internal
property
internal int OrderID { get; set; }
For the newer version of the library it seems that the property changed its name to be IncludePrivateMembers
You can now use the IncludePrivateMembers
property.
using CsvHelper;
using System.IO;
using System.Linq;
using Xunit;
namespace UseIncludePrivateMembers
{
public class Stub
{
public double Value1 { get; private set; }
public double Value2 { get; private set; }
}
public class Tests
{
[Fact]
public void Test()
{
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("Value1,Value2");
writer.WriteLine("5,6");
writer.Flush();
stream.Position = 0;
csv.Configuration.IncludePrivateMembers = true;
var record = csv.GetRecords<Stub>().First();
Assert.Equal(5, record.Value1);
Assert.Equal(6, record.Value2);
}
}
}
}
There is another way to fix this:
csv.Configuration.MemberTypes = CsvHelper.Configuration.MemberTypes.Fields;
The case for me may be slightly different, in that I am using fields instead of properties, but it was the same error message so...
Found the problem .. the properties had a private set .. they need to be public like this..
public int OrderID { get; set; }
public int DemandID { get; set; }
public string ExternalEventID { get; set; }
public int Part { get; set; }
public int BasedOnObjectID { get; set; }
public int BasedOnStateID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int? EventID { get; set; }