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? 3 16334
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?
<"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
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>
|
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...
|
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="">
|
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
{
|
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).
| |
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...
|
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...
|
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,
|
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...
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
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...
| |