Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Serialization and Events problem

mikeb
Guest
 
Posts: n/a
#1: Jul 22 '05
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?

Sherif El-Metainy
Guest
 
Posts: n/a
#2: Jul 22 '05

re: C# Serialization and Events problem


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" <mikeb@discussions.microsoft.com> wrote in message
news:A7E72E14-9CCB-4CD1-8C2E-633E04DF479D@microsoft.com...[color=blue]
>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?[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Jul 22 '05

re: C# Serialization and Events problem


<"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
mikeb
Guest
 
Posts: n/a
#4: Jul 22 '05

re: C# Serialization and Events problem


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

Mike B

"Sherif El-Metainy" wrote:
[color=blue]
> 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" <mikeb@discussions.microsoft.com> wrote in message
> news:A7E72E14-9CCB-4CD1-8C2E-633E04DF479D@microsoft.com...[color=green]
> >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?[/color]
>
>
>[/color]
Closed Thread