473,785 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ 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 16333
Hello

Prefix the NonSerializedAt tribute with "field:", this will make the C#
compiler mark the underlying delegate field and not the event itself as
NonSerialized.

[field:NonSerial ized] 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***@discussi ons.microsoft.c om> wrote in message
news:A7******** *************** ***********@mic rosoft.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 NonSerializedAt tribute with "field:", this will make the C#
compiler mark the underlying delegate field and not the event itself as
NonSerialized.

[field:NonSerial ized] 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.co m>
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 NonSerializedAt tribute with "field:", this will make the C#
compiler mark the underlying delegate field and not the event itself as
NonSerialized.

[field:NonSerial ized] 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***@discussi ons.microsoft.c om> wrote in message
news:A7******** *************** ***********@mic rosoft.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
1373
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> <title>Event test</title>
2
13406
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 8c4a969b-2aa4-4679-b170-d9f6441f7c6d, when I need it as {8A3ACA06-A7DE-41A5-B584-063E7CF391BB}, all upper, and with braces. Is there a way to force the XmlSerializer to serialize as the latter format? I tried to turn the field into a string property with private field...
1
4365
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 done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder="" manualname="">
4
1912
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: public class test: System.Windows.Forms.UserControl {
0
1217
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 through all the rows and build the XML string and store it in a local file (c:\temp\file1.htm) and then navigate to this local file. Each HTML row has name link (using line number as a name).
3
371
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. 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...
0
1144
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 that I want to serialize an instance of this type, pass it as a parameter to wp1 and then deserialize it. Let's say that type1 is a person´s info, containig attributes like name, age and so on and I want to show that info in the webpage. OK, the...
4
2138
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 Delete button on the end of each row. I am unable to gain access to the event when the button is clicked. I don't fully understand how the click gets connected to the C# code,
5
1451
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 here: http://www.timothyrosenberg.com/test1.html. If I put a javascript (like those that you get from flickr, feedigest, etc.) in the list that is controled by the YUI the list does not expand correctly and all of the <liunder it are forced open...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.