我试图映射一个 csv 文件,以便每条记录都只是一个Dictionary<string,object>
.
我收到一个
ArgumentException "不是成员访问;"
当我尝试这样做时。代码如下:
public class CsvFileReader : FileReader
{
public CsvFileReader(string path) : base(path){ }
public IDictionary<string, object> Read()
{
var reader = new CsvReader(new StreamReader(Path));
reader.Read();
reader.Configuration.RegisterClassMap(new DictionaryClassMap(reader.FieldHeaders));
return reader.GetRecord<Dictionary<string, object>>();
}
private class DictionaryClassMap : CsvClassMap<Dictionary<string, object>>
{
private readonly IEnumerable<string> _headers;
public DictionaryClassMap(IEnumerable<string> headers)
{
_headers = headers;
}
public override void CreateMap()
{
foreach (var header in _headers)
{
var localHeader = header;
Map(x => x[localHeader]);
}
}
}
}