Do not serialize delegates and events blindly

By Roy Osherove In .Net Original

Markdown version

Historical article · Originally published December 29, 2004; technical details may be outdated.

usually, you should not serialize your class's delegates or events. This is because serialization takes he full object graph into play, and delegates by nature will serialize your subscriber list into the mix (invocation list). you can never make sure all your subscribers are serializable, can you?

so, you should put [NonSerialized] on delegates.

on events (with the event keyword) you can use the

[Field:NonSerialized] attribute  ("Field" is in System.Reflection)

[via Juval Lowy's .Net Master class]

More in .Net Original

All posts in .Net Original