Davenull
March 23rd, 2009, 06:28 PM
Hi,
If i want to serialize an object with a transient member using, for instance, a binary formatter, and "reconstruct" the transient member when i deserialize the object, i've two solutions (as far i know)
1) using the IDeserializationCallback interface and implementing its OnDeserialization() method:
[Serializable]
class Truc : IDeserializationCallback {
private int limite;
[NonSerialized]
private int[] tab;
(...)
// Automatically called after Deserialize()
public virtual void OnDeserialization(Object sender) {
(...)
}
}
2) throwing away the interface and using the [OnDeserialized] attribute with the OnDeserialized() method:
[Serializable]
class Truc : [IDeserializationCallback {
private int limite;
[NonSerialized]
private int[] tab;
(...)
[OnDeserialized]
public void OnDeserialized(StreamingContext context) {
(...)
}
}
Both give the same result : the tab member is not serialized and is reconstructed after deserialization.
But i wonder if there is, somewhere, guidelines about which is the better way (for my own, i think the second is "cleaner", but it's purely subjective...)
If i want to serialize an object with a transient member using, for instance, a binary formatter, and "reconstruct" the transient member when i deserialize the object, i've two solutions (as far i know)
1) using the IDeserializationCallback interface and implementing its OnDeserialization() method:
[Serializable]
class Truc : IDeserializationCallback {
private int limite;
[NonSerialized]
private int[] tab;
(...)
// Automatically called after Deserialize()
public virtual void OnDeserialization(Object sender) {
(...)
}
}
2) throwing away the interface and using the [OnDeserialized] attribute with the OnDeserialized() method:
[Serializable]
class Truc : [IDeserializationCallback {
private int limite;
[NonSerialized]
private int[] tab;
(...)
[OnDeserialized]
public void OnDeserialized(StreamingContext context) {
(...)
}
}
Both give the same result : the tab member is not serialized and is reconstructed after deserialization.
But i wonder if there is, somewhere, guidelines about which is the better way (for my own, i think the second is "cleaner", but it's purely subjective...)