我有以下类(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();
    }
}

我创建了一个映射类,如 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>();
    }
}

当我上线时
return csv.GetRecords<EventObject>().ToList();

我得到一个异常(exception)

最佳答案

发现问题.. 属性有一个私有(private)集..
他们需要像这样公开..

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; }

关于csvhelper - 使用 csvHelper 没有为类型映射任何属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37521158/

10-12 21:48