Mattias: Well "Field" it actually is.
Compiles and all that too. Funnily enough, Resharper
flags it as bad code... but it still compiles
Roy,
Curiously enough, this seems to work in .NET 2.0 even
without the NonSerialized attribute being placed on
delegates. What's even curious-er, the subscriptions to
the delegates even are deserialized properly. Are you
saying we should stay away from that *feature* ?
- Sahil
Sahil: yeah - you should stay away from serializing and
deserializing your delegates, because the way
serializtion works, it will take the object and all the
fields it has and serialize them. It will *recursively*
go throuh each object's fields and serialize thos as
well. For evenets and delegates, this means that it will
go through all the current subscribers of the event and
serialize them as well. It *might* work, but what
happens when you have a class that cannot be serialized
that subscribes to your event? Your serialization will
fail. Since you can't *force* your subscribers to be
serializable (that's the whole idea with the publish
subscribe model - you never know who listens to your
events) you should always assume that they are not
playing nice, thus - you should avoid from serializing
them using the [nonserialized] attribute.
Roy make sense, but I've been trying hard to make
Serialization using the BinaryFormatter fail using the
[Serializable] attribute, but it always seems to work
LOL. I understand the infinitely recursive pattern,
though there is the situation where you serialize
something on one machine, transport it to another, and
deserialize where the subscribers aren't there .. then
what happens !! It still didn't fail LOL.
Anyway, I understand the argument, though in practice
I've been myself arguing with it in my head. Could it be
said that "Because it's so automatic, be scared
of anything that is so automatic since you don't really
know what it'll do cuz it has a mind of it's
own". .. I guess I'm stickin' to that theory.
Nice solution. I wasted a lot of time on this.
For Sahil: My business object was raising events that a
form was subscribing to. WinForm cannot be serialized,
or marked as such. Hope this will stop the argument in
your head!