Hello
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; } }
}
Best regards,
Sherif
"mikeb" <mi***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
I have a class (settings, etc.) which is derived from CollectionBase and
contains a number of fields and a few events. When I create an instance of
this class in my app, and serialize it, it works. If I add an event
handler
in my app (for one of the events in my settings class), I get an exception
that my app class is not marked as Serializable. If I mark the app class
as
Serializible, I get an exception that Type System.Windows.Forms.Form in
Assembly System.Windows.Form is not marked as Serializable. I am not
interested is serializing any data relating to the event, but it does not
seem possible to mark events/delegates as NonSerialized. Are events
serialized? How do I avoid this problem?