473,785 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NonSerialized attribute and events

I am using C# in Visual Studio 2003.
I would like to serialize a class that contains, amongst other things,

public delegate void DocumentsPrefer encesChange(obj ect env, Documents e);
public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

However, as it stands, the classes containing any methods/delegates that have been added to OnDocumentsPref erencesChange also have to be serializable: this I do not want.
I do not actually need OnDocumentsPref erencesChange serialized, so would like to be able to use the NonSerializable attribute as follows:

[NonSerializable] public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

Unfortunately, this gives the compile-time error:

'"Attribute 'NonSerialized; ' is not valid on this declaration type. It is valid on 'field' declarations only"

I can get round the problem by setting OnDocumentsPref erencesChange to null before serialization and restoring it afterwards, but this seems messy.

Is there a better way?

Any help would be appreciated.

Nov 16 '05 #1
5 15183
Serializing classes that have non-serializable members can be a pain. The
best way to overcome the problem is to implement the Memento pattern and
create a serializable object that represents your class without actually
serializing the target class itself.

Just google on "Memento Pattern"

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Neil Norfolk" <Neil No*****@discuss ions.microsoft. com> wrote in message
news:85******** *************** ***********@mic rosoft.com...
I am using C# in Visual Studio 2003.
I would like to serialize a class that contains, amongst other things,

public delegate void DocumentsPrefer encesChange(obj ect env, Documents e);
public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

However, as it stands, the classes containing any methods/delegates that have been added to OnDocumentsPref erencesChange also have to be
serializable: this I do not want. I do not actually need OnDocumentsPref erencesChange serialized, so would like to be able to use the NonSerializable attribute as follows:
[NonSerializable] public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;
Unfortunately, this gives the compile-time error:

'"Attribute 'NonSerialized; ' is not valid on this declaration type. It is valid on 'field' declarations only"
I can get round the problem by setting OnDocumentsPref erencesChange to null before serialization and restoring it afterwards, but this seems messy.
Is there a better way?

Any help would be appreciated.

Nov 16 '05 #2
Bob,

I have to disagree with this. You can apply the NonSerializable
attribute to the event, like so:

[field:NonSerial izable]
public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

This will prevent the event handlers from being serialized.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:OZ******** ******@TK2MSFTN GP12.phx.gbl...
Serializing classes that have non-serializable members can be a pain. The
best way to overcome the problem is to implement the Memento pattern and
create a serializable object that represents your class without actually
serializing the target class itself.

Just google on "Memento Pattern"

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Neil Norfolk" <Neil No*****@discuss ions.microsoft. com> wrote in message
news:85******** *************** ***********@mic rosoft.com...
I am using C# in Visual Studio 2003.
I would like to serialize a class that contains, amongst other things,

public delegate void DocumentsPrefer encesChange(obj ect env, Documents e); public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

However, as it stands, the classes containing any methods/delegates that have been added to OnDocumentsPref erencesChange also have to be
serializable: this I do not want.
I do not actually need OnDocumentsPref erencesChange serialized, so would

like to be able to use the NonSerializable attribute as follows:

[NonSerializable] public event DocumentsPrefer encesChange

OnDocumentsPref erencesChange;

Unfortunately, this gives the compile-time error:

'"Attribute 'NonSerialized; ' is not valid on this declaration type. It is valid on 'field' declarations only"

I can get round the problem by setting OnDocumentsPref erencesChange to

null before serialization and restoring it afterwards, but this seems

messy.
Is there a better way?

Any help would be appreciated.


Nov 16 '05 #3
Well, I agree with your disagreement because I just tried it out. However,
your answer, while brilliant, is so esoteric that it took me some time to
find out anything about this attribute target usage. It's certainly not in
MSDN anywhere.

This entry in Rockford Lohtka's blog
http://www.lhotka.net/WeBlog/PermaLi...9-e0d840e6de2c
does explain it and he says "In C# it is possible to use the field: target
on an attribute to tell the compiler to apply the attribute to the backing
field rather than the actual variable. This means we can use [field:
NonSerialized()] to declare an event, which will cause the backing delegate
field to be marked with the NonSerialized attribute. This is a bit of a
hack, but does provide a solution to the problem"

Anyway, Thanks for the education!

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Bob,

I have to disagree with this. You can apply the NonSerializable
attribute to the event, like so:

[field:NonSerial izable]
public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

This will prevent the event handlers from being serialized.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:OZ******** ******@TK2MSFTN GP12.phx.gbl...
Serializing classes that have non-serializable members can be a pain. The
best way to overcome the problem is to implement the Memento pattern and
create a serializable object that represents your class without actually
serializing the target class itself.

Just google on "Memento Pattern"

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Neil Norfolk" <Neil No*****@discuss ions.microsoft. com> wrote in message
news:85******** *************** ***********@mic rosoft.com...
I am using C# in Visual Studio 2003.
I would like to serialize a class that contains, amongst other things,

public delegate void DocumentsPrefer encesChange(obj ect env, Documents e); public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

However, as it stands, the classes containing any methods/delegates
that have been added to OnDocumentsPref erencesChange also have to be
serializable: this I do not want.
I do not actually need OnDocumentsPref erencesChange serialized, so
would like to be able to use the NonSerializable attribute as follows:

[NonSerializable] public event DocumentsPrefer encesChange

OnDocumentsPref erencesChange;

Unfortunately, this gives the compile-time error:

'"Attribute 'NonSerialized; ' is not valid on this declaration type. It

is
valid on 'field' declarations only"

I can get round the problem by setting OnDocumentsPref erencesChange to

null before serialization and restoring it afterwards, but this seems

messy.
Is there a better way?

Any help would be appreciated.



Nov 16 '05 #4
I also have a serialization issue and I'm trying to find the best way
to handle it. The Memento pattern may work, but I'm looking for other
suggestions.

My actual application is quite complex so I will simplify the problem
here in its basic form. In a nutshell, I have a tree of objects, each
object is a different node of the tree, like a treeview. All objects
inherit from my INode interface. INode provides a reference to one
parent object and a collection of child nodes (like treeview).

Some of the items in the tree inherit from IDependentObjec t. This is
an INode which contains a collection of other nodes on which this node
is dependent.

The question is how to do serialization/deserialization . Obviously we
don't want to serialize the references of an IDependentObjec t. My
current idea is to have a guid associated with each node. In an
IDependentObjec t, only the guids of the dependent objects would be
serialized/deserialized. Then whenever all deserialization is done,
we'd call a special routine to reconnect references based on the
guids. Please note that I only need serialization/deserialization for
file save/load.

I'm sure that this problem is common and I hope to receive some good
suggestions as to the best practice for solving this.

Thanks in advance.

Jim
Nov 16 '05 #5
An interesting and very useful set of answers.
I was attracted to the momento idea as its quite elegant and I have a general like of 'Gang of Four' patterns. However, it means that if the class to be serialized (class Child, say) appears as a member of another serializable class (class Parent, say), then Parent needs to use custom serialization (by implementing ISerializable) so that the momento of the Child can serialized/deserialized rather than the Child itself. Custom serialisation potentially involves a lot of code if Parent has many members that need serialization. However, custom serialization means that version control is easier (if an extra field is added, say, then the custom constructor can take appropriate action when reading a file created using an older version of the class).
The [field:NonSerial izable] idea is excellent for the particular problem I had.

"Bob Powell [MVP]" wrote:
Well, I agree with your disagreement because I just tried it out. However,
your answer, while brilliant, is so esoteric that it took me some time to
find out anything about this attribute target usage. It's certainly not in
MSDN anywhere.

This entry in Rockford Lohtka's blog
http://www.lhotka.net/WeBlog/PermaLi...9-e0d840e6de2c
does explain it and he says "In C# it is possible to use the field: target
on an attribute to tell the compiler to apply the attribute to the backing
field rather than the actual variable. This means we can use [field:
NonSerialized()] to declare an event, which will cause the backing delegate
field to be marked with the NonSerialized attribute. This is a bit of a
hack, but does provide a solution to the problem"

Anyway, Thanks for the education!

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Bob,

I have to disagree with this. You can apply the NonSerializable
attribute to the event, like so:

[field:NonSerial izable]
public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;

This will prevent the event handlers from being serialized.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:OZ******** ******@TK2MSFTN GP12.phx.gbl...
Serializing classes that have non-serializable members can be a pain. The best way to overcome the problem is to implement the Memento pattern and
create a serializable object that represents your class without actually
serializing the target class itself.

Just google on "Memento Pattern"

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Neil Norfolk" <Neil No*****@discuss ions.microsoft. com> wrote in message
news:85******** *************** ***********@mic rosoft.com...
> I am using C# in Visual Studio 2003.
> I would like to serialize a class that contains, amongst other things,
>
> public delegate void DocumentsPrefer encesChange(obj ect env, Documents

e);
> public event DocumentsPrefer encesChange OnDocumentsPref erencesChange;
>
> However, as it stands, the classes containing any methods/delegates that have been added to OnDocumentsPref erencesChange also have to be
serializable: this I do not want.
> I do not actually need OnDocumentsPref erencesChange serialized, so would like to be able to use the NonSerializable attribute as follows:
>
> [NonSerializable] public event DocumentsPrefer encesChange
OnDocumentsPref erencesChange;
>
> Unfortunately, this gives the compile-time error:
>
> '"Attribute 'NonSerialized; ' is not valid on this declaration type. It

is
valid on 'field' declarations only"
>
> I can get round the problem by setting OnDocumentsPref erencesChange to
null before serialization and restoring it afterwards, but this seems

messy.
>
> Is there a better way?
>
> Any help would be appreciated.
>
>
>



Nov 16 '05 #6

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

Similar topics

2
2202
by: greenxiar | last post by:
Hi, When i enumerate fields in class with reflection, How to know if a field is marked as NonSerialized? Type c2 = typeof(Class2); foreach(FieldInfo m in c2.GetFields (BindingFlags.NonPublic| BindingFlags.Instance| BindingFlags.GetField|
0
1470
by: expertware | last post by:
Dear friends, When an object is cloned through serialization in memory, to make a copy. clearly the <NonSerialized> fields are not copied. To copy them I usually use (this is a very useful simple Sub): '------------------ copying non serialized --------- Public Sub CopiaFieldsNotSerialized(ByVal Destinazione As Object, ByVal Origine As Object)
1
2600
by: Timo | last post by:
When I use XML serialization to serialize a collection of MyObject, all of the public fields of the object are being serialized, even those I've given the <NonSerialized()> attribute. <Serializable()> Public Class MyObject Public FieldID As String Public ParentId As String Public ItemId As String
3
11976
by: Oren | last post by:
Hi everyone I just want to ask why this code below adds the variables i,v to the serialization too ? I have made them as NonSerialized I'm using XmlSerializer and TextWriter classes to create xml file with an instance of class x any suggestions
1
1740
by: Alexander Muylaert | last post by:
How can I prevent an event of beïng serialized? it always give me the error Attribute nonserialized is not valid on this declaration type ... Kind regards Alexander
2
7276
by: Crispin Horsfield | last post by:
It's not useful to serialize events in a class that can be serialized so in C# you can use the 'field' attribute to mark events as 'NonSerializable'.This is not available in VB. Does anyone know when it might be? TIA Crispin Horsfield Caz Limited, Bristol, UK
5
1844
by: Samuel R. Neff | last post by:
Is there a way to add the NonSerialized attribute to the "Event" fields that the VB.NET compiler creates when you declare an event? I have a class that we use with binary serialization and we just added two events so now old files won't deserialize to the new class. It says it was expecting 2 more fields, obviously corresponding to the two private fields created for the events. I tried adding the fields explicitly with the...
2
4116
by: Timo | last post by:
When I use XML serialization to serialize MyCustomObjectCollection, all of the public fields of MyCustomObject are being serialized, even those I've given the <NonSerialized()> attribute. <Serializable()> Public Class MyCustomObject Public FieldID As String Public ParentId As String Public ItemId As String
1
1638
by: Ehsan | last post by:
Does this attribute really work? I'm trying to "deep" serialize one of my objects which has a member variable pointing to a Form object. I don't want to serialize this form (and I know that I can't do this anyway because the Form class is not marked as Serializable). I try marking this member variable with the <NonSerialized> attribute, but the BinaryFormatter seems to be ignoring this attribute; It throws an exeption "Class frmReport...
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
9480
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,...
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...
1
10091
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,...
0
8972
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, 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...
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.

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.