Serialization...

  • Thread starter Thread starter Atmapuri
  • Start date Start date
A

Atmapuri

Hi!

I would like to deserialize an object to which
other unknown objects hold multiple references.

Is it possible to deserialize the object without the
need to destroy and recreate it? How?

Thanks!
Atmapuri
 
Atmapuri,

When you deserialize an object, you aren't writing over an existing
object, you are creating a new object from information that was saved to a
stream.

Also, when you serialize an object, the process doesn't destroy the
object. It just "reads" the object (and all references) and stores that
information in a stream. You can continue to use the object after you have
serialized it to a stream.

Hope this helps.
 
Hi!
When you deserialize an object, you aren't writing over an existing
object, you are creating a new object from information that was saved to a
stream.

Also, when you serialize an object, the process doesn't destroy the
object. It just "reads" the object (and all references) and stores that
information in a stream. You can continue to use the object after you
have serialized it to a stream.

Hope this helps.

<g> Not really. The problem is that if I replace the object that I
deserialize, the rest of my application does not update the
pointers to it and continues to reference the old version.

Therefore, I hope that there is a way to deserialize an object in such a
way that it wont break the links.

For example, if you save the state of an application as a configuration
file using serialization, deserializing core objects in to new instances,
makes it very very difficult to update the links to those objects within
your app.

In extrem case you would have to specifiy at the command line, which
configuration file to use... instead of selecting from the menu, Load
settings..

Thanks!
Atmapuri
 
Atmapuri said:
<g> Not really. The problem is that if I replace the object that I
deserialize, the rest of my application does not update the
pointers to it and continues to reference the old version.

Maybe you have an object, say MObject, that serializes itself thus:
MObject.Save(MemoryStream ms);

and then deserializes itself thus:
MObject.Load(MemoryStream ms);

and maybe you find that MObject doesn't update to the newly loaded
serialized values. This being the case, simply make sure Load returns a new
instance of MObject, thus:
MObject = MObject.Load(MemoryStream ms);

in this way the MObject 'pointer' gets refreshed. Maybe that helps <g>
 
Hi!
Maybe you have an object, say MObject, that serializes itself thus:
MObject.Save(MemoryStream ms);

and then deserializes itself thus:
MObject.Load(MemoryStream ms);

and maybe you find that MObject doesn't update to the newly loaded
serialized values. This being the case, simply make sure Load returns a
new instance of MObject, thus:
MObject = MObject.Load(MemoryStream ms);

in this way the MObject 'pointer' gets refreshed. Maybe that helps <g>

I have multiple MyObject pointers throughout the app at more or less
unknown locations. If I return a new object that updates only one
reference and creates two problems:

1.) memory leak, because the old object is still referenced by other
objects.
2.) broken references, because other objects still reference the old
object.

Is there maybe a way to copy/clone values from one object to another
without replacing the other (Clone copy)? In this way I could deserialize
to a new object and then clone back to the old one...

Thanks!
Atmapuri
 
Atmapuri said:
I have multiple MyObject pointers throughout the app at more or less
unknown locations. If I return a new object that updates only one
reference and creates two problems:

This is a different situation to the one I presented and I agree that the
solution I suggested doesn't apply to it.
Is there maybe a way to copy/clone values from one object to another
without replacing the other (Clone copy)? In this way I could
deserialize to a new object and then clone back to the old one...

Implementing .NET's ICloneable interface may be useful to you here. Cloning
an object to another will certainly not replace the original.
 
Hi!
Implementing .NET's ICloneable interface may be useful to you here.
Cloning an object to another will certainly not replace the original.

That is what I was trying to avoid <g>. I am looking for a way to implement
a generic Save/Load method, which would work for all derived objects
regardless of their property count or type, following their RTTI
(attributes)
specs.

Serialization looks like it, except for that "minor" issue..

I just dont feel like going down the path of reinventing
serialization... :) or reimplementing ICloneable for every derived class
and then also keeping track of object versions, so that each time
I add/remove a property I also have to modify the streaming.

Thanks!
Atmapuri
 
Atmapuri said:
That is what I was trying to avoid <g>. I am looking for a way to
implement a generic Save/Load method, which would work for all
derived objects regardless of their property count or type, following
their RTTI (attributes)
specs.

Serialization looks like it, except for that "minor" issue..


Well, I personally feel that doing MObject.Load(MemoryStream ms); should be
sufficient to update the MObject reference (and those of all child objects
as well). Doing MObject = MObject.Load(MemoryStream ms); seems to be a
redundant, although necessary, call.

I'd also be interesting in hearing from anyone who's been able to do this
'generically', that is, without having to write a method to control custom
serialization and without having to write serialization code in every class.
I don't hold out a lot of hope though.

Good luck!
 
You are looking to overwrite an object that exists with serialization?
You can't do that. The easiest way for the serialization engine to
guarantee the validity of the data in the deserialized object would be to
handle the creation of the object.

For your solution, instead of passing the object around everywhere, you
should be getting the object from a well-known place, like a static property
on a class. Then, if you serialize/deserialize the member, you can just
discard what you were returning, and return your new instance.

Of course, this means that you have to patch up all of the calls on the
object reference that you have to reference the new well-known location, and
to not store references to the object.
 
Atmapuri said:
Hi!


<g> Not really. The problem is that if I replace the object that I
deserialize, the rest of my application does not update the
pointers to it and continues to reference the old version.

You have to wrap your object with another object. The references are to the
outer object. You create a new inner object from serialization.

For example you have a list of employees List<Employee>. You wrap this like

public class Employees
{
public List<Employee> List ...
}

so whereas before you held a reference to EmployeeList in multiple places in
your application you now hold a reference to Employees. List can be "new"ed
as you need.

Alternatively you could copy the values (i.e. primitive types) from a
deserialized object to the object that the references are held.

PS
 
Hi!
You are looking to overwrite an object that exists with serialization?
You can't do that. The easiest way for the serialization engine to
guarantee the validity of the data in the deserialized object would be to
handle the creation of the object.

Or let the user make any customizations. But unfortunately not
supported by default. Consequently, the .NET community is now in a need
of an alternative commercial serialization system that allows saving/loading
application contents without restarting it :)

Thanks!
Atmapuri
For your solution, instead of passing the object around everywhere, you
should be getting the object from a well-known place, like a static
property on a class. Then, if you serialize/deserialize the member, you
can just discard what you were returning, and return your new instance.

Of course, this means that you have to patch up all of the calls on the
object reference that you have to reference the new well-known location,
and to not store references to the object.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Atmapuri said:
Hi!


<g> Not really. The problem is that if I replace the object that I
deserialize, the rest of my application does not update the
pointers to it and continues to reference the old version.

Therefore, I hope that there is a way to deserialize an object in such a
way that it wont break the links.

For example, if you save the state of an application as a configuration
file using serialization, deserializing core objects in to new instances,
makes it very very difficult to update the links to those objects within
your app.

In extrem case you would have to specifiy at the command line, which
configuration file to use... instead of selecting from the menu, Load
settings..

Thanks!
Atmapuri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top