C# Serialization and Events problem

G

Guest

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?
 
S

Sherif El-Metainy

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
 
J

Jon Skeet [C# MVP]

<"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
net> said:
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; } }
}

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/threads/lockchoice.shtml
 
G

Guest

Thanks for the help. I used the field prefix for now and everything works fine.

Mike B

Sherif El-Metainy said:
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 said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top