473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

C# Serialization and Events problem

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?
Jul 21 '05 #1
3 16279
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?

Jul 21 '05 #2
<"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
net>> wrote:
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/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Thanks for the help. I used the field prefix for now and everything works fine.

Mike B

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


Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: VK | last post by:
To PointedEars with my deep respect. Author ;-) There were a question a while ago about events. Now I see that my original explanation was not full. Check this sample out: <html> <head>...
2
by: Jiho Han | last post by:
I generated a class from a schema. One of the fields are typed as System.Guid. Perfect. The only problem is when this class serializes, the guid field serializes as...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
4
by: Tamir Khason | last post by:
I have a control with custom events, fired by events occured from inside the control. But I can not see those events in design time from Properties window. What's wrong??? Following the code: ...
0
by: Sudhakar S P | last post by:
Using Microsoft Web Browser control with windows form in VB.NET 2003 I have problem getting events to work properly, can anyone help me here, 1. Reading the data from the database, looping...
3
by: mikeb | last post by:
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....
0
by: Miguel RS | last post by:
Hi all, I have a winforms app (app1), a webservice (ws1) and a webpage (wp1). The webservice exposes a type (type1). When I generate a proxy (px1) for app1 I get the type ws1.type1. The thing is...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
5
by: screaminmartin | last post by:
I have been trying to implement Dustin Diaz's YUI Custom Events code on my website. You can see his demo here: http://www.dustindiaz.com/basement/custom-events.html. My demo website can be found...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.