简体   繁体   中英

Binary deserialize a vb.net class in different .net project

I am sending some data from a 32 bit .net program over a socket connection (a remote desktop channel if you are wondering). I have to send some binary data (actually some basic email info + a file attachment for outlook). The email + subject is “text” data, but the file can/will be of course binary.

So I have a struct (or a class) like this:

<Serializable>
Public Class oEmail
   ' we serialize this into one Byte() object - send to client and then deserlize
 Public Email As String         ' outlook email
 Public Subject As String       ' outlook subject
 Public CC As String            ' outlook CC
 Public BC As String            ' outlook bc (blind copy)
 Public MessageText As String   ' message text
 Public AttachPath As String    ' path name to attachment (with \)
 Public AttachFileName As String
 Public FileDat As New Byte()    ' any kind of windows file - to be attached to outlook
End Class

Now, binary serialization “would” be the cat's meow. I can take the above structure – serialize it to a byte array and simply send it down the network pipe to the client and then in-cork it.

Eg:

Dim MyOutlook As New oEmail
MyOutlook = Deserialize(Of oEmail)(MyData)

And the deserialize code is REALLY simple:

Public Shared Function Deserialize(Of T)(ByVal data As Byte()) As T
        Dim binFormat = New BinaryFormatter()

        Using M As New IO.MemoryStream(data, False)
            Return CType((New BinaryFormatter).Deserialize(M), T)
        End Using

End Function

However, on the client side, it is a 64 bit application. It is also .net but called by the RDP client as a external un-managed .dll which in turn runs the managed code. To make a bit of a REALLY long story short, I can't add or consume the SAME .dll for the above class structure on the client software side.

So, when I try to deserialize on the client side I of course get this BEYOND nasty message:

Unable to find assembly 'RDPSERVER' ; Version 1.0.0.0

It is “beyond” sad that .net does not allow binary serialization of a class UNLESS you use the EXACT same .dll and assembly. (why some “loose” serialization method is not provided is beyond me – but I not looking to rant here!). I mean, take the byte() data - map it back out to the class or struct!

Is there any way to “override” the EXACT assembly requirement and mis-match issue?

Oh so simple WHEN one can share the .dll. I can however share the source code between the two applications (client and server).

Of course above gives the assembly miss-match.

I am open to considering the XML serializer (but will it transfer binary data such as a file intact?). And the XML serialize does not create a byte() array either.

Is there a way to override the “exact assembly” requirement that .net requires with binary serialization?

The code is vb.net, but if a simple c# bit of code allows overriding of the assembly name, I am open to c# code.

Because of assembly miss-match (and wanting to avoid a external dll). I would up using XML serlization. The following two routines do work, but increases the size of the byte() data sent by a factor of 3

   Public Shared Function SerializeXML(ByVal data As Object) As Byte()
        Dim ser As XmlSerializer = New XmlSerializer(data.GetType)
        Dim xBuff As New StringWriter

        ser.Serialize(xBuff, data)

        Return System.Text.Encoding.Unicode.GetBytes(xBuff.ToString)
    End Function


    Public Shared Function DeSerializeXML(Of T)(ByVal data As Byte()) As T
        Dim ser As New XmlSerializer(GetType(T))
        Dim XbUF As New MemoryStream
        XbUF.Write(data, 0, data.Length)
        XbUF.Position = 0
        Return ser.Deserialize(XbUF)
    End Function

So once the data() buffer is sent down the RDP channel, then I use this:

       Dim MyEmailLoc As New oEmail

        MyEmailLoc = DeSerializeXML(Of oEmail)(MyData)

The above thus gives me the ability to send the class object down the network pipe but with significant overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM