473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Serializing an object declared withevents in a nonserializable object

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
2 4024
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get...
2
by: Giovanni Bassi | last post by:
Hello All, I have encountered a problem. I am using visual inheritance and my base form adds an event handler on Form Load using the AddHandler Keyword. The problem is that if the Event...
9
by: Remulac | last post by:
Hello, I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object reference not set...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
0
by: Deepak C.G via .NET 247 | last post by:
I want to dispose the image object in my child form, unless I won't dispose this object i can't delete the image file in my folder. I get this error in MDIparent form "An unhandled exception...
2
by: Jakob Bengtsson | last post by:
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...
8
by: ST | last post by:
Hello everyone, Can anyone help me with this error above when I debug my web app project in vstudio.net?? I can't figure it out! It was working fine for months, and now all of a sudden it's not!!...
13
by: | last post by:
Hi all, Coming from the good old VB6 days we were told to always destroy our objects as follows:- Dim obj as New MyObject ' do some work with obj obj = Nothing I have been doing this in...
4
by: Joergen Bech | last post by:
I sometimes use delegates for broadcasting "StateChanged" events, i.e. if I have multiple forms and/or controls that need updating at the same time as the result of a change in a global/common...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.