473,386 Members | 1,799 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,386 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 4025
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.