Object deserialization issue

G

Guest

I have a class marked as [Serializable]. This class contains private members
that I don't need to serialize, as such I've tagged their declaration as
[NonSerialized]. Most of these members are initialized in the constructor of
my class.

The serialization and deserialization works fine BUT, when deserializing
(hence re-creating objects graph) I'm loosing these non-serialized members
initialization.
I thought that the object constructor would be executed anyhow, which
doesn't seem to be the case.

Here is my code snippet:

[Serializable]
public class Satellite
{
private String ip; // This member will be serialized
private int port; // This member will be serialized
[NonSerialized] private String _status; // Volatile info, no need to be
serialized
[NonSerialized] private int _statusCode; // Volatile info, no need to be
serialized

public void Satellite()
{
this._status = "Unknown";
this._statusCode = 0;
}
}

Any idea, comment or documentation pointer is welcomed.
Thanks in advance.
Arno
 
R

Robert Jordan

Arno said:
I have a class marked as [Serializable]. This class contains private members
that I don't need to serialize, as such I've tagged their declaration as
[NonSerialized]. Most of these members are initialized in the constructor of
my class.

The serialization and deserialization works fine BUT, when deserializing
(hence re-creating objects graph) I'm loosing these non-serialized members
initialization.
I thought that the object constructor would be executed anyhow, which
doesn't seem to be the case.

Implement IDeserializationCallback and reinitialize the [NonSerialized]
members using IDeserializationCallback.OnDeserialization.

bye
Rob
 
G

Guest

Many thanks Rob. It works just fine !
Arno said:
I have a class marked as [Serializable]. This class contains private members
that I don't need to serialize, as such I've tagged their declaration as
[NonSerialized]. Most of these members are initialized in the constructor of
my class.

The serialization and deserialization works fine BUT, when deserializing
(hence re-creating objects graph) I'm loosing these non-serialized members
initialization.
I thought that the object constructor would be executed anyhow, which
doesn't seem to be the case.

Implement IDeserializationCallback and reinitialize the [NonSerialized]
members using IDeserializationCallback.OnDeserialization.

bye
Rob
 

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

Top