<"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
net>> wrote:[color=blue]
> Prefix the NonSerializedAttribute with "field:", this will make the C#
> compiler mark the underlying delegate field and not the event itself as
> NonSerialized.
>
> [field:NonSerialized] public event MyEventHandler MyEvent;
>
> Another workaround is using custom events (although I think the first
> solution is simpler)
>
> [NonSerialized]
> private MyEventHandler _myEvent;
> public event MyEventHandler MyEvent
> {
> add { _myEvent += value; } // Not Thread-safe
> remove { _myEvent -= value; } // Not Thread-safe
> }
>
> Or use the thread safe version if your event can be accessed from multiple
> threads
>
> [NonSerialized]
> private MyEventHandler _myEvent;
> public event MyEventHandler MyEvent
> {
> add { lock(this) {_myEvent += value; } }
> remove { lock(this) {_myEvent -= value; } }
> }[/color]
One suggestion for the thread-safe version - rather than locking on
"this" (which is almost always a bad idea) lock on a privately owned
object.
See
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too