一种优雅的方法来严格“序列化/反序列化”我的类属性值(通过字节数组)?

时间:2014-01-11 03:30:11

标签: vb.net serialization deserialization

“紧”是指最短的字节数组。

以下代码完美无缺,但我相信它可能会更优雅。 如代码注释中所示,反序列化部分存在效率低下。

我不知道能够将我的propertyList元素作为引用类型而不是值类型。

如果有可能做我想要的事情,那么ayone可以建议代码吗?

Imports System
Public Class Form1

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim myPerson, deserialisedPerson As Person
    Dim serialisedProperties As Byte()

    myPerson = New Person("John Smith", "26")
    serialisedProperties = myPerson.Serialise

    deserialisedPerson = New Person(serialisedProperties)
 End Sub
End Class


Class Person
 Public Name As String = ""
 Public Age As Integer = 0

 Private _propertyList As New List(Of Object)
 Private _numberOfBytesList As New List(Of Integer)

 Sub New(ByVal name As String, ByVal age As Integer)
    Me.Age = age
    Me.Name = Left(name.PadRight(24), 24)  'strings will be truncated/padded to  24 characters
    UpdatePropertiesList()
 End Sub

 Sub New(ByVal serialisedProperties As Byte())
    'this alternative constructor deserialises the byte array
    Dim tempBytes As Byte()
    Dim byteCounter As Integer = 0
    UpdatePropertiesList()

    For index As Integer = 0 To _propertyList.Count - 1
        'load tempBytes with relevent bytes for this property
        ReDim tempBytes(_numberOfBytesList(index) - 1)
        Array.ConstrainedCopy(serialisedProperties, byteCounter, tempBytes, 0, tempBytes.Length)

        Select Case _propertyList(index).GetType.Name
            Case "String"
                _propertyList(index) = System.Text.Encoding.Unicode.GetString(tempBytes).TrimEnd
                'HOW CAN WE GET PROPERTY REFERENCES INTO   _propertyList (INSTEAD OF VALUES) 
                'SO THAT THIS SINGLE LINE OF CODE WILL UPDATE     deSerialisedperson.Name FROM "" to "John Smith"?
            Case "Int32"
                _propertyList(index) = BitConverter.ToUInt32(tempBytes, 0)
                'HOW CAN WE GET PROPERTY REFERENCES INTO   _propertyList (INSTEAD OF VALUES) 
                'SO THAT THIS SINGLE LINE OF CODE WILL UPDATE     deSerialisedperson.Age FROM 0 to "26"?
            Case Else
                Beep()
        End Select
        byteCounter += _numberOfBytesList(index)
    Next

    'THESE LINES OF CODE COULD BE AVOIDED IF I COULD ACHIEVE THE ABOVE !@#
    Me.Name = _propertyList(0)
    Me.Age = _propertyList(1)
 End Sub

 Public Function Serialise() As Byte()
    Dim bytes As New List(Of Byte)

    For Each prop As Object In _propertyList
        Select Case prop.GetType.Name
            Case "String"
                bytes.AddRange(Text.Encoding.Unicode.GetBytes(CType(prop, String)))
            Case "Int32"
                bytes.AddRange(BitConverter.GetBytes(CType(prop, Int32)))
            Case Else
                Beep()
        End Select
    Next
    Return bytes.ToArray
 End Function

 Private Sub UpdatePropertiesList()
    _propertyList.Clear()
    _numberOfBytesList.Clear()

    _propertyList.Add(Name)
    _numberOfBytesList.Add(48) 'ie 24 unicode chars
    _propertyList.Add(Age)
    _numberOfBytesList.Add(4) 'ie int32
 End Sub

End Class

0 个答案:

没有答案