Live Chat Icon For mobile
Live Chat Icon

How come some of the objects are not initialized during deserialization?

Platform: WinForms| Category: General IO

This is possible in this case: Say your object graph contains an object A, which has a reference to the object B. Then while deserializing A, the reference B might not be initialized yet. This is because while deserializing, references are deserialized one at a time and when A is deserialized, B might not have been deserialized, yet. You should follow the workaround as follows:


[C#]
protected MyCustomConstrucotr(SerializationInfo info, StreamingContext context)
{
	this.cachedRefToB = (B)info.GetValue('B', typeof(B));
	// At this point cachedRefToB might not be initialized.
}
// But when this method gets called, after complete serialization, the cachedRefToB will be initialized
void IDeserializationCallback.OnDeserialization(object sender)
{
	// At this point cachedRefToB will be initialized.
}

[VB.Net]
protected MyCustomConstrucotr(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected
	Me.cachedRefToB = CType(info.GetValue('B', Type.GetType(B)), B)
	’ At this point cachedRefToB might not be initialized.
End Function

’ But when this method gets called, after complete serialization, the cachedRefToB will be initialized
’ Your class should implement IDeserializationCallback
Sub OnDeserialization(ByVal sender As Object) as IDeserializationCallback.OnDeserialization
	’ At this point cachedRefToB will be initialized.
End Sub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.