473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 16297
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.