Serializing an object declared withevents in a nonserializable object

  • Thread starter Thread starter Jakob Bengtsson
  • Start date Start date
J

Jakob Bengtsson

Hi,

I have a form (which cannot be serialized).

In the form's code I declare an object like this (never
mind the object nor class name, it's for illustration
only):

Private WithEvents eventPublisher as EventPublisherClass

When I try to serialize my eventPublisher object, I get an
error. It would seem, that the binary formatter I'm using
is trying to serialize all subscribers to the
eventPublisher's events, which in this case can't be done
(the subscriber is a form, which cannot be serialized).

Here's how I serialize the object

Dim f as new BinaryFormatter
Dim ms as new io.memorystream

try
f.serialize(ms, eventPublisher)
' Code here to persist the memory stream somewhere...
Catch ex as exception
stop
Finally
ms.close
End Try

Is there any way to serialize my eventPublisher?

Thanks for any help,
Jakob
 
Hi Jakob,

For security issue, you may not deserialize a delegate. For now You could
use the workaround, that is to implement ISerializable yourself.

Here is my sample code, You may have a test.
<Serializable()> _
Class DataWithEvent
Implements ISerializable
Public Event EventHandler()
Public Function hello()
RaiseEvent EventHandler()
End Function
Public x As Int32
Private Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
x = info.GetInt32("x")
End Sub
Public Sub New()
x = 100
End Sub
Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As
StreamingContext) Implements ISerializable.GetObjectData
info.AddValue("x", x)
End Sub
End Class
Dim WithEvents dwe As New DataWithEvent
Sub Main()
Dim m As New MemoryStream
Dim b As New BinaryFormatter
Dim dwc As DataWithEvent
dwe.hello()
b.Serialize(m, dwe)
m.Position = 0
dwc = b.Deserialize(m)
Console.WriteLine(dwc.x)
End Sub
Private Sub dwe_EventHandler() Handles dwe.EventHandler
Console.WriteLine("Event fired")
End Sub


You have a try and let me know if it does the job for you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Hi Peter,

Thanks alot -- this seems to work fine :)

I just have one question: In the GetObjectData sub, shall
I simply use the info.addValue("property", Value) for all
properties and fields in my class that I wish to
serialize?

Thanks again,
Jakob
-----Original Message-----
Hi Jakob,

For security issue, you may not deserialize a delegate. For now You could
use the workaround, that is to implement ISerializable yourself.

Here is my sample code, You may have a test.
<Serializable()> _
Class DataWithEvent
Implements ISerializable
Public Event EventHandler()
Public Function hello()
RaiseEvent EventHandler()
End Function
Public x As Int32
Private Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
x = info.GetInt32("x")
End Sub
Public Sub New()
x = 100
End Sub
Sub GetObjectData(ByVal info As
SerializationInfo, ByVal context As
 
Back
Top