I have the following class
public class EventObject
{
public int OrderID { get; private set; }
public int DemandID { get; private set; }
public string ExternalEventID { get; private set; }
public int Part { get; private set; }
public int BasedOnObjectID { get; private set; }
public int BasedOnStateID { get; private set; }
public DateTime StartDate { get; private set; }
public DateTime EndDate { get; private set; }
public int? EventID { get; private set; }
public static IEnumerable<EventObject> LoadFromCSV(TextReader reader)
{
var plannedEventsToReturn = new List<EventObject>();
var csv = new CsvReader(reader);
csv.Configuration.RegisterClassMap<EventObjectMap >();
return csv.GetRecords<EventObject>().ToList();
}
}
and I have created a mapping class as documentd in csvHElper
public sealed class EventObjectMap : CsvClassMap<EventObject>
{
public EventObjectMap ()
{
Map(m => m.OrderID).Index(0);
Map(m => m.DemandID).Index(1);
Map(m => m.ExternalEventID).Index(2);
Map(m => m.Part).Index(3);
Map(m => m.BasedOnObjectID).Index(4);
Map(m => m.BasedOnStateID).Index(5);
Map(m => m.StartDate).Index(6).TypeConverter<OptimizationDateTimeConverter>();
Map(m => m.EndDate).Index(7).TypeConverter<OptimizationDateTimeConverter>();
Map(m => m.EventID).Index(8).TypeConverter<NullableIntConverter>();
}
}
when I hit the line
return csv.GetRecords<EventObject>().ToList();
i get an exception
no properties are mapped for type
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; }
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With