我正在将CSVHelper从v2.2.2迁移到v7.1.1,并遇到以下错误:-
No mappings were specified in the CsvClassMap
...在此行上调用“ RegisterClassMap”时:-
csvReader.Configuration.RegisterClassMap(classCsvClassMap);
查看Configuration.cs文件中的“ RegisterClassMap”方法...(https://github.com/JoshClose/CsvHelper/blob/master/src/CsvHelper/Configuration/Configuration.cs)
我可以看到该类映射未注册,因为它不包含任何MemberMaps或ReferenceMaps
/// <summary>
/// Registers the class map.
/// </summary>
/// <param name="map">The class map to register.</param>
public virtual void RegisterClassMap(ClassMap map)
{
if (map.MemberMaps.Count == 0 && map.ReferenceMaps.Count == 0)
{
throw new ConfigurationException("No mappings were specified in the CsvClassMap.");
}
Maps.Add(map);
}
在版本2.2.2中,尽管没有任何MemberMaps或ReferenceMaps,但使用相同的方法“ ExtractClaimRows”注册了classMap。我没有从v2.2.2中获得“ Configuration.cs”文件,因此无法看到它是如何实现的。
我的问题:-
尼克,有帮助的人,这里是方法。
public static List<T> ExtractClaimRows<T>(
byte[] fileInMemory, string csvSeparator, string fileEncoding,
ClaimRowCsvClassMap<T> classCsvClassMap)
{
List<T> records = null;
using (var ms = new MemoryStream(fileInMemory))
{
try
{
// load csv file
var csvReader =
new CsvReader(new StreamReader(ms,
ToolsEncoding.ConvertToRealEncoding(fileEncoding), true));
csvReader.Configuration.RegisterClassMap(classCsvClassMap);
csvReader.Configuration.TrimOptions = TrimOptions.Trim;
csvReader.Configuration.ShouldSkipRecord = record =>
record.All(string.IsNullOrEmpty);
csvReader.Configuration.TrimOptions = TrimOptions.Trim;
csvReader.Configuration.Delimiter = csvSeparator;
csvReader.Configuration.Encoding =
ToolsEncoding.ConvertToRealEncoding(fileEncoding);
csvReader.Configuration.PrepareHeaderForMatch = header =>
header.ToLower(); // to validate
records = csvReader.GetRecords<T>().ToList(); // get all
records
}
catch (Exception ex)
{
Log.Error(ex);
}
}
return records;
}