2013/01/30(Wed) 11:57:58 編集(投稿者)
開始位置のパラメータを追加した別建てのメソッドを作成しました。
以下のような感じでしょうか。
#前述の話題ですが、Midだから+1ですよね。substringでしたら0スタートなので+1の心配はしなくてよいということをすっかり失念してました。
Dim a5 As AAA = StructureFromText(Of AAA)(strW)
Dim b5 As BBB = StructureFromText(Of BBB)(strW, Len(a5))
'指定された文字列を、VBFixedString 属性の付いたフィールドに順次格納した値を返します。
Public Function StructureFromText(Of T As Structure)(ByVal str As String, ByVal intStart As Integer) As T
'RuntimeHelpers.GetObjectValue 対策の為、ValueType で受ける
Dim newValue As ValueType = StructureFromText
Dim pos As Integer = 1
For Each f As FieldInfo In newValue.GetType().GetFields()
Dim attributes() As Object = f.GetCustomAttributes(GetType(VBFixedStringAttribute), False)
If attributes.Length <> 0 Then
Dim attr As VBFixedStringAttribute = DirectCast(attributes(0), VBFixedStringAttribute)
Dim value As String = Strings.Mid(str & StrDup(attr.Length, " "c), intStart + pos, attr.Length)
f.SetValue(newValue, value)
pos += attr.Length
End If
Next
Return DirectCast(newValue, T)
End Function