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

Home Posts Topics Members FAQ

Catching creation/destruction of serializable objects

I need to maintain certain data structure, shared by all instances of
a class, declared as [Serializable]. This data structure must change
whenever a new object is created or an existing object is destroyed.
So I declared a static field with [NonSerialized()] attribute.

Now I need to add some code into all places where objects are created
and destroyed. This class has 2 ctors: one default, and another taking
parameters. I've found out, to my surprise, that new instances are
created without the use of these 2 ctors (how?).

My question: where should I put my code that tracks object creation
and destruction for these serializable objects?
Jun 27 '08 #1
5 1286
Maybe you should have a "Data Structure" type class to which you add
multiple instances of your other class, adding/removing/reordering items in
the list changes the data structure.
Jun 27 '08 #2
On Tue, 27 May 2008 14:24:12 -0700, <cc*******@gmai l.comwrote:
I need to maintain certain data structure, shared by all instances of
a class, declared as [Serializable]. This data structure must change
whenever a new object is created or an existing object is destroyed.
So I declared a static field with [NonSerialized()] attribute.

Now I need to add some code into all places where objects are created
and destroyed. This class has 2 ctors: one default, and another taking
parameters. I've found out, to my surprise, that new instances are
created without the use of these 2 ctors (how?).

My question: where should I put my code that tracks object creation
and destruction for these serializable objects?
Well, I would have said the constructor. I'm puzzled by your description,
as my experience with the basic serialization API in .NET is that it
requires a default constructor to deserialize objects, and that
constructor is in fact called when an object is deserialized.

You seem to be having the opposite experience. It would probably be
helpful if you would post a concise-but-complete code sample that
demonstrates the problem.

Pete
Jun 27 '08 #3
On May 27, 2:36 pm, "Peter Morris" <mrpmorris at gmail dot comwrote:
Maybe you should have a "Data Structure" type class to which you add
multiple instances of your other class, adding/removing/reordering items in
the list changes the data structure.
If I were creating all this from scratch, I'd have done so.

But I've got quite a lot of existing code that uses these serializable
objects directly, so I want to keep the changes to a minimum. And for
that, I need to identify all places where such objects are created and
destroyed. I thought that it would be easier to do from inside this
class.
Jun 27 '08 #4
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
>My question: where should I put my code that tracks object creation
and destruction for these serializable objects?

Well, I would have said the constructor. I'm puzzled by your description,
as my experience with the basic serialization API in .NET is that it
requires a default constructor to deserialize objects, and that
constructor is in fact called when an object is deserialized.
I think it depends on what type of serialization is being used. Binary
serialization (using the BinaryFormatter class) does NOT call the
constructor when deserializing. Although I've not tried it myself, I believe
you can mark a method to be called during or after deserialization by using
the OnDeserializing Attribute or OnDeserializedA ttribute attributes. Try
searching for help on these attributes for more details.

Chris Jobson
Jun 27 '08 #5
Chris Jobson's post should get you sorted - but a minor point;
serialization applies to instances, so I doubt that the [NonSerialized]
on a *static* field makes any difference.

Marc
Jun 27 '08 #6

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

Similar topics

7
5655
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text data. The class is contains these private variables:
3
1702
by: owatona | last post by:
Hi to all, I would like to know how .net manage the objects and what are the cicle of life of the objects... Are the same this cicle, for the objects created in windows applications and for the web applications? Any help will be grateful Thank Owatona
2
2368
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
31
3419
by: Tom P. | last post by:
I am doing quite a bit of custom painting and it means I have to create a lot of brushes (think one for every file system object in a directory) per paint. How expensive is this? Should I find a way to create the brushes once, store them in an member variable, and use them when I need them? Or is creating brushes a throw-away process that doesn't take a lot of work? Thanks for the info. Tom P.
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
10329
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...
1
10092
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
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...
0
8974
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
7500
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
6740
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();...
1
4053
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
3650
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.