472,950 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

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 1254
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*******@gmail.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*********@nnowslpianmk.comwrote 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 OnDeserializingAttribute or OnDeserializedAttribute 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
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...
3
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...
2
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
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.