Click to See Complete Forum and Search --> : About attributes


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...)

boudino
March 24th, 2009, 04:07 AM
I think, that if you have a method (OnDeserialized()) which says how to solve the task, it is better to do it using interface, because it is imperative, in oppose of declarative based on attributes. If you'd say what to do, but don't suggest s solution, than an attribute is better way.