/// <summary> /// Serializes an object into a SerializationInfo object. /// </summary> /// <param name="obj">Object to be serialized.</param> /// <returns></returns> public SerializationInfoContainer SerializeObject(object obj) { SerializationInfoContainer info; if (obj == null) { if (!this._serializationReferences.TryGetValue(NullPlaceholder.Instance, out info)) { info = new SerializationInfoContainer(this._serializationReferences.Count + 1) { TypeName = NullPlaceholder.TypeShortName }; this._serializationReferences.Add(NullPlaceholder.Instance, info); } } else { var thisType = obj.GetType(); //if (thisType.IsSerializable) //{ // throw new InvalidOperationException(string.Format("Object Not Serializable Formatted: {0}", thisType.FullName)); //} var mobile = obj as IMobileObject; if (mobile == null) { throw new InvalidOperationException(string.Format("{0} must implement IMobileObject", thisType.Name)); } if (!this._serializationReferences.TryGetValue(mobile, out info)) { info = new SerializationInfoContainer(this._serializationReferences.Count + 1); this._serializationReferences.Add(mobile, info); info.TypeName = thisType.AssemblyQualifiedName; var context = new SerializationContainerContext(info, this); mobile.SerializeRef(context); context.IsProcessingState = true; mobile.SerializeState(context); } } return(info); }
private object DeserializeCore(List <SerializationInfoContainer> deserialized) { this._deserializationReferences = new Dictionary <int, IMobileObject>(); var context = new SerializationContainerContext(null, this); context.IsProcessingState = true; foreach (SerializationInfoContainer info in deserialized) { if (info.TypeName == NullPlaceholder.TypeShortName) { this._deserializationReferences.Add(info.ReferenceId, null); } else { Type type = Type.GetType(info.TypeName); if (type == null) { throw new SerializationException(string.Format("MobileFormatter unable to deserialize {0}", info.TypeName)); } IMobileObject mobile = null; try { mobile = (IMobileObject)Activator.CreateInstance(type, true); } catch (MissingMethodException) { throw new InvalidOperationException(string.Format("类型 {0} 必须拥有无参数的构造函数才能支持序列化!", type.FullName)); } this._deserializationReferences.Add(info.ReferenceId, mobile); context.Container = info; mobile.DeserializeState(context); } } context.IsProcessingState = false; foreach (SerializationInfoContainer info in deserialized) { IMobileObject mobile = this._deserializationReferences[info.ReferenceId]; if (mobile != null) { context.Container = info; mobile.DeserializeRef(context); } } foreach (SerializationInfoContainer info in deserialized) { var notifiable = this._deserializationReferences[info.ReferenceId] as ISerializationNotification; if (notifiable != null) { context.Container = info; notifiable.Deserialized(context); } } return(this._deserializationReferences.Count > 0 ? this._deserializationReferences[1] : null); }