使用csvHelper为类型映射没有属性

时间:2016-05-30 08:37:46

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

我创建了一个在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();

我得到例外

  

没有为类型

映射属性

4 个答案:

答案 0 :(得分:9)

发现问题..属性有私有集.. 他们需要像这样公开......

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 :(得分:3)

因此,当接受的答案正常工作时,还有另一种解决此问题的方法,如下所示

CsvConfiguration configuration = new CsvConfiguration
{
   IgnorePrivateAccessor = true,
}

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

Documentation

注意即使您拥有internal属性

,也可能遇到同样的问题
internal int OrderID { get; set; }

答案 2 :(得分:0)

您现在可以使用IncludePrivateMembers属性。

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

答案 3 :(得分:0)

还有另一种解决方法:

csv.Configuration.MemberTypes = CsvHelper.Configuration.MemberTypes.Fields;

对我来说情况可能略有不同,因为我使用的是字段而不是属性,但这是相同的错误消息,所以...