Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDeserializationCallback vs OnDeserializedAttribute

As far as I understand, the IDeserializationCallback interface and the OnDeserialized event can both be used when an object needs to perform some task after being deserialized.

IDeserializationCallback:

[Serializable]
public class Foo : IDeserializationCallback
{
    public void OnDeserialization(object sender)
    {
         // initialize unserialized fields etc.
    }
}

OnDeserialized event:

[Serializable]
public class Foo
{
    [OnDeserialized]
    public void OnDeserialized(StreamingContext context)
    {
         // initialize unserialized fields etc.
    }
}

Are there any specific pros/cons or scenarios where you would choose one over the other?

like image 711
Erik Öjebo Avatar asked Aug 20 '09 19:08

Erik Öjebo


1 Answers

Those two serve different purposes and cannot be used interchangeably. In most of the cases you probably will be better served by the interface.

Look here for some explanation: http://social.msdn.microsoft.com/Forums/en-US/netfxremoting/thread/311b2b57-6b0a-49ed-aa96-84f69d51da0f

like image 200
Val Avatar answered Oct 12 '22 12:10

Val