使用 FileHelpers 库在 VB.NET 中做一些很棒的事情。使用从文本文件模板构建的动态类解析文件。我找不到的一件事:一种读取单个记录并确定它应该导致生成两条记录的方法。
当前代码:
Dim FromType As Type = Dynamic.ClassBuilder.ClassFromSourceFile(MyFilePath, MyDynamicTypeName, NetLanguage.VbNet)
Dim FromRecords() As Object FromRecords = FileHelpers.CommonEngine.ReadString(FromType, MyStringBuilder.ToString)
'... 也许这里的代码来检查某些值
昏暗引擎作为新的 FileTransformEngine(Of ITransformable(Of MyDestinationClass), MyDestinationClass)
' 理想情况下,在下一行中,我希望它能够查看某些条件并能够从单个源行生成两条记录。Dim PayRecords() As Object = engine.TransformRecords(FromRecords)
或者,如果有办法实现“ITransformable(Of ...”TransformTo() 并让它返回多条记录,我可以将逻辑放在动态类定义 TransformTo() 方法中。
想法?
这是我的源动态类的示例:
导入 FileHelpers ' 永远不会忘记
_ Public NotInheritable 类 MyDynamicClass 实现 ITransformable(Of MyDestinationClass) _ Public Name As String
<FieldQuoted(""""c, QuoteMode.OptionalForRead, MultilineMode.AllowForRead)> _
Public KeyType As String
Public Hours As Double
公共函数 TransformTo() As MyDestinationClass 实现 ITransformable(Of MyDestinationClass).TransformTo Dim res As New 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(...))
结束函数结束类