468,533 Members | 1,922 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,533 developers. It's quick & easy.

Hooking events post deserialisation

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?

Nov 24 '06 #1
7 1726
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

Hi,ni***********@iinet.net.au wrote:
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?

Nov 24 '06 #2
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot

On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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 Tijsmahttp://www.e-office.com

Hi,nick.fletc...@iinet.net.au wrote:
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?
Nov 26 '06 #3
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
ni***********@iinet.net.au wrote:
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot

On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
>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 Tijsmahttp://www.e-office.com

Hi,nick.fletc...@iinet.net.au wrote:
>>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?
Nov 26 '06 #4
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

On Nov 27, 3:29 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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

nick.fletc...@iinet.net.au wrote:
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot
On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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 Tijsmahttp://www.e-office.com
Hi,nick.fletc...@iinet.net.au wrote:
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?
Nov 26 '06 #5
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
ni***********@iinet.net.au wrote:
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

On Nov 27, 3:29 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
>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

nick.fletc...@iinet.net.au wrote:
>>I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot
On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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 Tijsmahttp://www.e-office.com
Hi,nick.fletc...@iinet.net.au wrote:
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?
Nov 28 '06 #6
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 :-)

On Nov 28, 11:37 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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 Tijsmahttp://www.e-office.com

nick.fletc...@iinet.net.au wrote:
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
On Nov 27, 3:29 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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
nick.fletc...@iinet.net.au wrote:
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot
On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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 Tijsmahttp://www.e-office.com
Hi,nick.fletc...@iinet.net.au wrote:
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?
Nov 28 '06 #7
Ah, so are you using the XmlSerializer then?
ni***********@iinet.net.au wrote:
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 :-)

On Nov 28, 11:37 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
>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 Tijsmahttp://www.e-office.com

nick.fletc...@iinet.net.au wrote:
>>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
On Nov 27, 3:29 am, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
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
nick.fletc...@iinet.net.au wrote:
I thought so much :-( Thanks for the hint on the interface though -
I'll give that a shot
On Nov 24, 10:14 pm, Wiebe Tijsma <wiebeREM...@CAPITALStijsma.com>
wrote:
>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 Tijsmahttp://www.e-office.com
>Hi,nick.fletc...@iinet.net.au wrote:
>>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?
Nov 29 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by drewnoakes | last post: by
3 posts views Thread by Rick Strahl [MVP] | last post: by
reply views Thread by Carl Clawson | last post: by
7 posts views Thread by Yovi Oktofianus | last post: by
1 post views Thread by John Dann | last post: by
3 posts views Thread by Jim | last post: by
5 posts views Thread by Ryan Ginstrom | last post: by
8 posts views Thread by pigeonrandle | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.