By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

Serializing an object declared withevents in a nonserializable object

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

--------------------
Content-Class: urn:content-classes:message
From: "Jakob Bengtsson" <j.*********@email.dk>
Sender: "Jakob Bengtsson" <j.*********@email.dk>
Subject: Serializing an object declared withevents in a nonserializable objectDate: Mon, 18 Aug 2003 01:25:00 -0700
Lines: 34
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNlYjcOwYQSf+iWQEuDOgMBAtd+3Q==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:104858
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.general

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


Jul 19 '05 #2

P: n/a
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 coulduse 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 AsStreamingContext)
x = info.GetInt32("x")
End Sub
Public Sub New()
x = 100
End Sub
Sub GetObjectData(ByVal info As SerializationInfo, ByVal context AsStreamingContext) 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.
--------------------
Content-Class: urn:content-classes:message
From: "Jakob Bengtsson" <j.*********@email.dk>
Sender: "Jakob Bengtsson" <j.*********@email.dk>
Subject: Serializing an object declared withevents in a nonserializable
object
Date: Mon, 18 Aug 2003 01:25:00 -0700
Lines: 34
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNlYjcOwYQSf+iWQEuDOgMBAtd+3Q==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.general:104858NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.general

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 anerror. It would seem, that the binary formatter I'm usingis 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


.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.