FileHelpers用泛型控制/详细记录
本文关键字:记录 控制 泛型 FileHelpers | 更新日期: 2023-09-27 17:53:58
我在获取主细节记录与泛型一起工作时遇到了问题。我的代码如下
// Setup file engine with Master/Detail types
MasterDetailEngine<PurchaseOrderHeader, PurchaseOrderLine> engine = new MasterDetailEngine<PurchaseOrderHeader, PurchaseOrderLine>(CommonSelector.MasterIfBegins, "H");
// Init the Master/Detail
MasterDetails<PurchaseOrderHeader, PurchaseOrderLine> poRecs = new MasterDetails<PurchaseOrderHeader, PurchaseOrderLine>();
// Setup list for detail lines & populate with test data
List<PurchaseOrderLine> poLines = new List<PurchaseOrderLine>();
for (int i = 1; i <= 5; i++)
{
poLines.Add(new PurchaseOrderLine() { LineMarker = "L", LineNumber = i, ItemCode = "TestCode", Price = 12.34M, Quantity = i * 2, UOM = "EA" });
}
// Attach records to Master/Detail
poRecs.Master = new PurchaseOrderHeader() { LineMarker = "H", PayloadID = "3223232@ariba.acme.com", CustomerPO = "DO1234", ShipToAddressCode = "ShipToAddressCode", BillToAddressCode = "BillToAddressCode" };
poRecs.Details = poLines.ToArray();
// Write file
engine.WriteFile("SampleFile.txt", poRecs);
我得到以下编译错误:
CS1502:最佳重载方法匹配
FileHelpers.MasterDetail.MasterDetailEngine<ImportExport.PurchaseOrderHeader,ImportExport.PurchaseOrderLine>.WriteFile(string,System.Collections.Generic.IEnumerable<FileHelpers.MasterDetail.MasterDetails<ImportExport.PurchaseOrderHeader,ImportExport.PurchaseOrderLine>>)
有一些无效参数。
我试过将poRecs
铸造为IEnumerable
,但仍然不起作用。
任何建议都将非常感谢!
看起来对WriteFile
的调用期望IEnumerable<MasterDetails<PurchaseOrderHeader, PurchaseOrderLine>>
,而您只是传递MasterDetails<PurchaseOrderHeader, PurchaseOrderLine>
。
当然你不能将MasterDetails
转换为IEnumerable
,因为它们是不可转换的。您需要做的是用IEnumerable
包装poRecs
。试试以下命令:
engine.WriteFile("SampleFile.txt", new[] { poRecs });