By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

How to serialize in "destructor" ?

P: n/a
I have a class which has a member variable of the type System.ArrayList.
I plan on having the ArrayList serialize/deserialize itself to/from XML. I
have worked out the code to do this and have the ArrayList is currently
deserializing in the constructor. When is the correct time to serialize the
ArrayList to XML? In C++ I would do this in the destructor. I see C# has a
destructor, but I am unsure about all of this IDisposable, Finalize, Dispose
and such. When would I perform cleanup code? Could someone explain these
terms to me ? Are there any source examples on MSDN to explain this ?
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Trevor Balcom" <tb*****@attbi.co> wrote in message news:<#e*************@TK2MSFTNGP10.phx.gbl>...
I have a class which has a member variable of the type System.ArrayList.
I plan on having the ArrayList serialize/deserialize itself to/from XML. I
have worked out the code to do this and have the ArrayList is currently
deserializing in the constructor. When is the correct time to serialize the
ArrayList to XML? In C++ I would do this in the destructor. I see C# has a
destructor, but I am unsure about all of this IDisposable, Finalize, Dispose
and such. When would I perform cleanup code? Could someone explain these
terms to me ? Are there any source examples on MSDN to explain this ?


You shouldn't even consider seralizing in the finalizer. As far as I know, when
the finalizer is executed, you can't guarantee that all objects that would have
been part of your object graph would still be available in memory. You might
consider seralizing explicitly before program termination. Or periodically if
that is not possible.

You might want to further explain the current porblem that you are trying to
solve here.

-LM
Nov 15 '05 #2

P: n/a
Lucean Morningside <m@exquisitor.com> wrote:
You shouldn't even consider seralizing in the finalizer. As far as I know, when
the finalizer is executed, you can't guarantee that all objects that would have
been part of your object graph would still be available in memory.


I believe you can - they're still strongly reachable while your object
is strongly reachable. However, they may have been finalized before you
have, and generally not be in a very useful state. I agree it's not a
good idea to do anything like serialization in the finalizer though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.