|
-
March 23rd, 2009, 06:28 PM
#1
About attributes
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:
Code:
[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:
Code:
[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...)
Last edited by Davenull; March 23rd, 2009 at 06:30 PM.
-
March 24th, 2009, 04:07 AM
#2
Re: About attributes
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.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|