2011-02-23 41 views
0

使用FileHelpers庫在VB.NET中完成一些偉大的事情。用文本文件模板構建的動態類解析文件。有一件事我找不到:一種閱讀單個記錄並確定它應該導致產生兩個記錄的方法。
當前代碼:FileHelpers Library - 在轉換時追加多個記錄

昏暗FromType爲類型= Dynamic.ClassBuilder.ClassFromSourceFile(MyFilePath,MyDynamicTypeName,NetLanguage.VbNet)

昏暗FromRecords()作爲對象 FromRecords = FileHelpers.CommonEngine.ReadString(FromType,MyStringBuilder的ToString)

」 ......也許這裏的代碼來檢查某些值

昏暗引擎作爲新FileTransformEngine(中ITransformable(中MyDestinationClass),MyDestinationClass)

'理想情況下,在下一行中,我希望它能看到某些條件,並能夠從一個源代碼行生成兩條記錄。或者,如果有一種方法來實現「ITransformable(Of ...」)TransformTo()並讓它返回多條記錄,我可以把這個?邏輯在動態類定義TransformTo()方法

思想

這是我的源動態類的一個示例:

進口FileHelpers'永遠不要忘記

_ 公共NotInheritable類MyDynamicClass 實現ITransformable(中MyDestinationClass) _ 公共名稱作爲字符串

<FieldQuoted(""""c, QuoteMode.OptionalForRead, MultilineMode.AllowForRead)> _ 
Public KeyType As String 

Public Hours As Double 

公共功能TransformTo()作爲MyDestinationClass器具ITransformable(中MyDestinationClass).TransformTo 昏暗的資源作爲新MyDestinationClass

res.ContactName = Name 
    ' Here is where I would like to say... instead of Return res 
    If KeyType="ABCD" Then 
     Dim newRes as New MyDestinationClass 
     newRes.Contactname = Name + " 2nd contact" 

     Dim resArray() as MyDestinationClass 
     redim resArray(1) 
     resArray(0) = res 
     resArray(1) = newRes 
    End If 
    Return resArray 
    ' Or alternately refer to the engine, but it is not in scope for the dynamic record (is it?). Something like... 
    engine.AppendToDestination(new MyDestinationClass(...)) 

結束功能 結束類別

回答

0

我認爲你要碰到的主要問題是ITransformable.TransformTo()被指定返回一個T值。

當您調用engine.TransformRecords()時,將在循環中調用此方法,其中爲每個輸入記錄添加一個輸出記錄。

如果你不介意做自己的FileTransformEngine版本,我不認爲這將是一個大的改變,但我沒有看到一個乾淨的方式來做到這一點。