How do you stop Events being Serialized?

N

Neal Andrews

Hi All,

Does anyone know how to stop Events from being serialized in a class that
uses the <Serializable()> attribute?
I have tried using the <NonSerialized()> attribute but for some bizarre
reason, it is not allowed on events.
I am using VS 2003, if that makes any difference. The only thing I can think
of is to wrap all the events in a separate class and then not to mark the
class as Serializable.

TIA
Regards
Neal
 
T

Tom Spink

Hi Neal, may I ask what the problem is with events being serialized?

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
N

Neal Andrews

Hi Tom,

I have a lot Business objects that need to be serialized across the network.
These objects each have a set of events. However when the Business objects
get serialized it also serialize's the the objects attached to the events. I
need a way to stop the objects attached to the events from being serialized.
I have just seen some C# code that uses the [field: NonSerialized] attribute
so that events do not get serialized. So there must be an equivalent
attribute in VB.NET, but I cannot seam to find it anyware.

Regards
Neal
 
R

Richard A. Lowe

With this (somewhat odd) attribute syntax:

[field: NonSerialized]
public event EventHandler SomethingHappens;

HTH,
Richard
 
J

Jay B. Harlow [MVP - Outlook]

Neal,
Unfortunately VB.NET does not support the NonSerialized attribute on events
(VB.NET does not have a Field Attribute Modifier, as shown in your other
post.).

The three methods I know of are:
1. Define your events in a C# base class that defines the event as being not
serializable (via [field: NonSerializable] or event procedures).
2. Implement the ISerializable interface and serialize only actual fields.
3. Create a SerializationSurrogate object that only serializes the actual
fields.

For details on the third method see the "Possible Solution to Serializing
Event Handlers in VB" thread from 10 Oct 2003 in the
microsoft.public.dotnet.distributed_apps &
microsoft.public.dotnet.framework.remoting newsgroups.

Because I need ISerializable for version control, I'm using the second
method.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Richard,
Not supported in current versions of VB.NET!

Unless of course you are saying in a C# base class.

Hope this helps
Jay

Richard A. Lowe said:
With this (somewhat odd) attribute syntax:

[field: NonSerialized]
public event EventHandler SomethingHappens;

HTH,
Richard

"Neal Andrews" <neal@NOSPAN_ALIAS.com> wrote in message
Hi All,

Does anyone know how to stop Events from being serialized in a class that
uses the <Serializable()> attribute?
I have tried using the <NonSerialized()> attribute but for some bizarre
reason, it is not allowed on events.
I am using VS 2003, if that makes any difference. The only thing I can think
of is to wrap all the events in a separate class and then not to mark the
class as Serializable.

TIA
Regards
Neal
 
N

Neal Andrews

Thanks a lot, At least I can stop looking for something that does not exist.
I'll look into those suggestions you made.

Regards
Neal

Jay B. Harlow said:
Neal,
Unfortunately VB.NET does not support the NonSerialized attribute on events
(VB.NET does not have a Field Attribute Modifier, as shown in your other
post.).

The three methods I know of are:
1. Define your events in a C# base class that defines the event as being not
serializable (via [field: NonSerializable] or event procedures).
2. Implement the ISerializable interface and serialize only actual fields.
3. Create a SerializationSurrogate object that only serializes the actual
fields.

For details on the third method see the "Possible Solution to Serializing
Event Handlers in VB" thread from 10 Oct 2003 in the
microsoft.public.dotnet.distributed_apps &
microsoft.public.dotnet.framework.remoting newsgroups.

Because I need ISerializable for version control, I'm using the second
method.

Hope this helps
Jay

Neal Andrews said:
Hi All,

Does anyone know how to stop Events from being serialized in a class that
uses the <Serializable()> attribute?
I have tried using the <NonSerialized()> attribute but for some bizarre
reason, it is not allowed on events.
I am using VS 2003, if that makes any difference. The only thing I can think
of is to wrap all the events in a separate class and then not to mark the
class as Serializable.

TIA
Regards
Neal
 
P

Peter Huang [MSFT]

Hi Neal,

As for Jay's second suggestion, you may try to take a look at the link
below.
ISerializable Interface
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeSeriali
zationISerializableClassTopic.asp

Hope this helps.

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.
--------------------
From: "Neal Andrews" <neal@NOSPAN_ALIAS.com>
References: <[email protected]>
Subject: Re: How do you stop Events being Serialized?
Date: Wed, 15 Oct 2003 20:19:00 +0100
Lines: 57
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: modem-636.lemur.dialup.pol.co.uk 217.135.130.124
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:147002
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Thanks a lot, At least I can stop looking for something that does not exist.
I'll look into those suggestions you made.

Regards
Neal

Jay B. Harlow said:
Neal,
Unfortunately VB.NET does not support the NonSerialized attribute on events
(VB.NET does not have a Field Attribute Modifier, as shown in your other
post.).

The three methods I know of are:
1. Define your events in a C# base class that defines the event as being not
serializable (via [field: NonSerializable] or event procedures).
2. Implement the ISerializable interface and serialize only actual fields.
3. Create a SerializationSurrogate object that only serializes the actual
fields.

For details on the third method see the "Possible Solution to Serializing
Event Handlers in VB" thread from 10 Oct 2003 in the
microsoft.public.dotnet.distributed_apps &
microsoft.public.dotnet.framework.remoting newsgroups.

Because I need ISerializable for version control, I'm using the second
method.

Hope this helps
Jay

Neal Andrews said:
Hi All,

Does anyone know how to stop Events from being serialized in a class that
uses the <Serializable()> attribute?
I have tried using the <NonSerialized()> attribute but for some bizarre
reason, it is not allowed on events.
I am using VS 2003, if that makes any difference. The only thing I can think
of is to wrap all the events in a separate class and then not to mark the
class as Serializable.

TIA
Regards
Neal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top