471,317 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 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 1184
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 discussion thread is closed

Replies have been disabled for this discussion.

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.