Object deserialization issue

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
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
 
Back
Top