mobile formatter - deserialize method

mobile formatter - deserialize method

Old forum URL: forums.lhotka.net/forums/t/5330.aspx


GA30 posted on Friday, August 29, 2008

Hello Everyone!

I have a situation whereby I have a parent object that has a set of child objects. These child objects in turn refer back to the parent object via a reference field. Currently the parent object manually serializes/deserialzes the child collection via OnGetChildren/OnSetChildren respectively. In OnSetChildren I was hoping to loop through the child collection and update each child's parent circular reference. However, I noticed that at this time the collection was empty. In order to get around this I modified the Deserialize method such that it calls OnSetChildren in reverse order. It works but I was wondering if I am causing some bad side effect here. Does anyone know? I really try to avoid modifying the framework. Thanks

public object Deserialize(XmlReader reader)

{

DataContractSerializer dc = new DataContractSerializer(

typeof(List<SerializationInfo>),

new Type[] { typeof(List<int>), typeof(byte[]) });

List<SerializationInfo> deserialized = dc.ReadObject(reader) as List<SerializationInfo>;

_deserializationReferences = new Dictionary<int, IMobileObject>();

foreach (SerializationInfo info in deserialized)

{

Type type = Type.GetType(info.TypeName);

if(type == null)

throw new SerializationException(string.Format(

"The Type '{0}' was unable to be deserialized, double check that the assembly containing this class has the same name on the Client and Server and that it is referenced by your server application",

info.TypeName));

#if SILVERLIGHT

IMobileObject mobile = (IMobileObject)Activator.CreateInstance(type);

#else

IMobileObject mobile = (IMobileObject)Activator.CreateInstance(type, true);

#endif

_deserializationReferences.Add(info.ReferenceId, mobile);

mobile.SetState(info);

}

//GA: set children in reverse order

//so that by the time the parent is encountered

//all children will be available

for (int i = deserialized.Count - 1; i >= 0; i--)

{

SerializationInfo info = deserializedIdea [I];

IMobileObject mobile = _deserializationReferences[info.ReferenceId];

mobile.SetChildren(info, this);

ISerializationNotification notifiable = mobile as ISerializationNotification;

if (notifiable != null)

notifiable.Deserialized();

}

//foreach (SerializationInfo info in deserialized)

//{

// IMobileObject mobile = _deserializationReferences[info.ReferenceId];

// mobile.SetChildren(info, this);

// ISerializationNotification notifiable = mobile as ISerializationNotification;

// if (notifiable != null)

// notifiable.Deserialized();

//}

return _deserializationReferences[1];

}

RockfordLhotka replied on Sunday, August 31, 2008

The order of deserialization is not guaranteed by any formatter (not ours or Microsoft's).

If you want to rehook events or reset object references you should override OnDeserialized(). This method is invoked for all three formatters and is the place for this sort of thing.

Copyright (c) Marimer LLC