Hooking events post deserialisation

  • Thread starter Thread starter nick.fletcher
  • Start date Start date
N

nick.fletcher

I have a custom collection which derives from Collection<> which stores
a number of objects. Before each item is added to the collection - an
event which it exposes is hooked by the collection and the re-fired to
its parent.

eg

class MyCollection : Collection<MyType>
{
public AddAnObject(MyType obj)
{
obj.SomeEvent += new eventhandler(obj_somethingChanged);
base.Add(obj);
}

private void obj_somethingChanged(object sender, EventArgs e)
{
Do something.....
}
}

The problem I have is that if you serialise and then deserialise the
collection, the events are no longer connected. You could probably fix
this in the deserialisation constructor but Im using CF and this doesnt
support System.Runtime.Serialisation. I can fix it by manually going
through the collection after it has been created and re-connecting but
does anyone know a better way?
 
Hi Nick,

I'm afraid you will have to reattach the events:

Implementing IDeserializationCallback on your collection will allow you
to get a callback as soon as deserialization is complete.

Regards,

Wiebe Tijsma
http://www.e-office.com
 
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot
 
Hi Nick,

You're welcome :)

If I may give an additional advise:

In my opinion, in most situations where events are used, they can be
replaced by a different architectural design using interfaces or
abstract base classes, especially on scenarios where serialization is
involved.

(events are evil :p)

Regards,

Wiebe Tijsma
 
Still no joy!

IDeserializationCallback is unfortunately a
System.Runtime.Serialization interface and is not supported under CF.
Wrt your last post - how could I change the architecture such that one
class can asynchronously notify another of an event via an
interface/abstract class. I mean, I could also use a delegate but I end
up with the same problem in that serialization will only serialize
public properties

Many thanks

Nick
 
Hi Nick,

I'm sorry for the wrong information then, I don't use the CF.

Are you using the XmlSerializer? this will only serialize public properties.

The binary serialization will serialize the in-memory representation of
your objects (fields), not the public properties.

What you could do is implement IEnumerable on your object model, and
iterate through your graph querying your object model for your own
custom interface that requires the callback (IMyCallBack) and execute
that recursively as soon as deserialization is complete.

In that case you can also reattach your events.

Best regards,

Wiebe Tijsma
http://www.e-office.com
 
Hiya

I actually found a solution

When the collection which holds the objects is deserialized it actually
calls it Add() method on the base class. All I needed to do was
override the add instead of wrapping it and hey presto

Thanks for your help though :-)
 
Ah, so are you using the XmlSerializer then?


Hiya

I actually found a solution

When the collection which holds the objects is deserialized it actually
calls it Add() method on the base class. All I needed to do was
override the add instead of wrapping it and hey presto

Thanks for your help though :-)
 

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

Back
Top