使用BinaryWriter序列化锯齿状数组的有效方法

时间:2015-07-05 19:49:10

标签: serialization jagged-arrays binarywriter

stackoverflowers。

我基本上想知道是否有人有关于我应该怎么做的提示。我已经使用BinaryWriter序列化常规基元类型(通过长度前缀字节数组)。 通常,我只是将另一个实体添加到我所拥有的primitivetypes字典中,但我希望能够序列化一个不定的锯齿状数组;如果它是System.Byte[][]System.Byte[][][][][][]

并不重要

我需要一种方法来为所有数组添加前缀'长度,也是一种告诉解串器有多少数组数组的方法。

我只想到一个很好的方法来做到这一点,但我一整天都在努力。

只是我提出的一个片段(它根本没有完整,但也许你得到了我的概念方法);

     Private Sub WriteArray(Writer As BinaryWriter, Type As Byte, Array As Array)
    Writer.Write(Array.GetLength(0)) 'Writing length so we know how many arrays there are.
    Dim Current As Type = Array.GetType() 'Getting the type of the (jagged-)array
    If Current.HasElementType AndAlso Current.GetElementType.IsArray Then 'Checking whether it's a jagged-array.
        Writer.Write(True) 'Writing true; which represents that it's a jagged-array.
        For Each A As Array In Array
            WriteArray(Writer, Type, A) 'Looping through the same method until...
        Next
    Else '... It's not jagged. -- This way is problematic; most of the times there are multiple jagged-arrays.
        Writer.Write(False) 'Not jagged.
        Select Case Type
            Case 0 '0 = byte, I use other bytes for strings/integers/etc.
                For Each Obj As Byte In Array
                    Writer.Write(Obj) 'Because it's not jagged we can finally write the bytes
                Next
        End Select
    End If
End Sub

希望看到你参与讨论,我们可能会提出解决方案。

0 个答案:

没有答案
相关问题