2016-05-30 7 views
7

ho la seguente classeottenendo immobili vengono mappate per il tipo di utilizzo csvHelper

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

e ho creato una classe di mappatura come 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>(); 
    } 
} 

quando ho colpito la linea

return csv.GetRecords<EventObject>().ToList(); 

ottengo un'eccezione

immobili sono mappati per il tipo

+0

ottengo questo anche con proprietà pubbliche – user3791372

risposta

8

trovato il problema .. le proprietà avevano un set privato .. hanno bisogno di essere pubblico come questo ..

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

Così come il accettato risposta funziona correttamente, v'è un altro modo per risolvere questo problema nel modo seguente

CsvConfiguration configuration = new CsvConfiguration 
{ 
    IgnorePrivateAccessor = true, 
} 

CsvReader reader = new CsvReader(/*your TextReader*/, configuration); 

Documentation

NOTA si potrebbe affrontare lo stesso problema, anche se si dispone di internal proprietà

internal int OrderID { get; set; }