PC Review


Reply
Thread Tools Rate Thread

Binary Serialization of Events

 
 
=?Utf-8?B?RGF2ZSBCb29rZXI=?=
Guest
Posts: n/a
 
      17th Nov 2005
I have a class with some public event fields. It appears that when I try to
serialize this class using a BinaryFormatter, the BinaryFormatter also tries
to serialize all the objects subscribed to the event. Is that right?

If that is what's going on, I don't want it to try to serialize subscribers
to the event. I tried marking the event with the [Nonserialized] attribute
but the compiler won't allow that. Any solutions?

Thanks
 
Reply With Quote
 
 
 
 
=?Utf-8?B?bmlja2R1?=
Guest
Posts: n/a
 
      17th Nov 2005
I had the same experience. I think the only solution is to implement
ISerializable and in GetObjectData() you only add the data you want
serialized.
--
Thanks,
Nick


"Dave Booker" wrote:

> I have a class with some public event fields. It appears that when I try to
> serialize this class using a BinaryFormatter, the BinaryFormatter also tries
> to serialize all the objects subscribed to the event. Is that right?
>
> If that is what's going on, I don't want it to try to serialize subscribers
> to the event. I tried marking the event with the [Nonserialized] attribute
> but the compiler won't allow that. Any solutions?
>
> Thanks

 
Reply With Quote
 
alantolan@users.com
Guest
Posts: n/a
 
      17th Nov 2005


if working in C# you should be able to mark the event as non-serialized

e.g.

[field: NonSerialized]
public event EventHandler MyEvent;


I working in VB.Net...unfortunately, up as far as VS2003, one could not
mark an event as NonSerialized.
I do not know if this has changed in VS2005.

You might try creating a base class in c# containing that event and
then inheiriting in from you VB.Net class.


hth,
Alan.

 
Reply With Quote
 
Richard Grimes
Guest
Posts: n/a
 
      17th Nov 2005
Dave Booker wrote:
> I have a class with some public event fields. It appears that when I
> try to serialize this class using a BinaryFormatter, the
> BinaryFormatter also tries to serialize all the objects subscribed to
> the event. Is that right?


Yes, wonderful isn't it? <g> When you serialize an object all the fields
are serialise and this includes the delegate that the compiler adds for
the event. (The event itself is just metadata.) When the runtime
serializes a delegate it serializes the target of that delegate. All the
delegates that are used for events are MulticastDelegate which means
that they contain a linked list of delegates and hence this linked list
is serialized.

You can do funky things with this like serializing an object in a file
or a database and then raising the event days later when you deserialize
the object. The deserialized object does not have to be on the same
machine!

> If that is what's going on, I don't want it to try to serialize
> subscribers to the event. I tried marking the event with the
> [Nonserialized] attribute but the compiler won't allow that. Any
> solutions?


Yes, you can disambiguate the attribute. Remember that the compiler uses
the event keyword to generate code:

- metedata for the event
- a delegate field
- methods to add and remove delegates

So all you need to do is tell the compiler to apply the attribute to the
appropriate one:

[field: NonSerialized]
public event MyEventType MyEvent;

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A bug in .Net Binary Serialization? ztRon Microsoft C# .NET 7 3rd Jul 2008 03:41 PM
Binary Serialization shapper Microsoft ASP .NET 2 10th Mar 2007 06:12 PM
Binary serialization =?Utf-8?B?SmFjcXVlcw==?= Microsoft Dot NET 15 28th Jun 2006 01:50 PM
binary vs. xml serialization Alex D. Microsoft ASP .NET 4 21st Dec 2005 05:47 PM
Binary Serialization over the net =?Utf-8?B?R2lsYWQgS2FwZWx1c2huaWs=?= Microsoft C# .NET 1 7th Nov 2005 02:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:46 AM.