FileHelpers ライブラリを使用して、VB.NET でいくつかの素晴らしいことを行います。テキスト ファイル テンプレートから構築された動的クラスを使用してファイルを解析します。見つけられないことが 1 つあります。単一のレコードを読み取り、その結果として 2 つのレコードが生成されると判断する方法です。
現在のコード:
Dim FromType As Type = Dynamic.ClassBuilder.ClassFromSourceFile(MyFilePath, MyDynamicTypeName, NetLanguage.VbNet)
Dim FromRecords() オブジェクトとして FromRecords = FileHelpers.CommonEngine.ReadString(FromType, MyStringBuilder.ToString)
'...おそらく、特定の値をチェックするためのコードがここにあります
Dim engine As New FileTransformEngine(Of ITransformable(Of MyDestinationClass), MyDestinationClass)
' 理想的には、この次の行で特定の条件を確認し、1 つのソース行から 2 つのレコードを生成できるようにしたいと考えています。Dim PayRecords() As Object = engine.TransformRecords(FromRecords)
あるいは、"ITransformable(Of ..." TransformTo() を実装して複数のレコードを返す方法があれば、動的クラス定義の TransformTo() メソッドにロジックを入れることができます。
考え?
ソース動的クラスのサンプルを次に示します。
FileHelpers をインポートします
_ パブリック NotInheritable クラス MyDynamicClass は ITransformable(Of MyDestinationClass) を実装します _ 文字列としてのパブリック名
<FieldQuoted(""""c, QuoteMode.OptionalForRead, MultilineMode.AllowForRead)> _
Public KeyType As String
Public Hours As Double
Public Function 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(...))
終了関数終了クラス