我想知道我是否做得对,但我正在使用CsvHelper并定义了以下流利的类映射:
internal class WasteEstimationGenericDayCsvMap : CsvClassMap<WasteEstimationGenericDay>
{
public WasteEstimationGenericDayCsvMap(WasteEstimationGenericDay dayModel)
{
var qualityModel = dayModel.Quality;
ConstructUsing(() => new WasteEstimationGenericDay(dayModel.ModelUnitContext));
Map(m => m.InflowVolume).Name(dayModel.GetPropertyDisplayName(() => dayModel.InflowVolume));
References<WasteStreamQualityCsvMap>(m => m.Quality, qualityModel);
}
}
internal class WasteStreamQualityCsvMap : CsvClassMap<WasteStreamQuality>
{
public WasteStreamQualityCsvMap(WasteStreamQuality qualityModel)
{
ConstructUsing(() => new WasteStreamQuality(qualityModel.ModelUnitContext));
Map(m => m.TotalNitrogen).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.TotalNitrogen));
Map(m => m.TotalPhosphorus).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.TotalPhosphorus));
Map(m => m.TotalDissolvedSalts).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.TotalDissolvedSalts));
Map(m => m.VolatileSolids).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.VolatileSolids));
Map(m => m.TotalSolids).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.TotalSolids));
Map(m => m.ElectricalConductivity).Name(qualityModel.GetPropertyDisplayName(() => qualityModel.ElectricalConductivity));
}
}
这些类映射旨在能够以不特定顺序将动态列名映射到我的类型WasteEstimationGenericDay
,但是问题是在将值映射到类型之前我需要指定这些值所在的单位,这就是为什么ConstructUsing()
在每个类中指定map 在构建映射类型的过程中设置单位。
WasteStreamQuality
但是,对于属性 ConstructUsing() 上的类型的引用类映射,WasteEstimationGenericDay.Quality, the
永远不会调用语句,也不会指定单位。有谁知道我在映射配置中是否做错了什么?或者这是CsvHelper中的错误/限制?
我还尝试在配置上分别注册两个类映射,如下所示:
csvReaderConfig.RegisterClassMap(new WasteEstimationGenericDayCsvMap(dayModel));
csvReaderConfig.RegisterClassMap(new WasteStreamQualityCsvMap(dayModel.Quality));
但它似乎没有任何影响,除非它是调用时指定的类型CsvReader.GetRecords<T>()
。
谢谢你的帮助。