using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.Serialization; using System.Xml.Schema; using System.Xml.Serialization; namespace Netron.Diagramming.Core { /// /// Complementary partial class related to (de)serialization. /// [Serializable] public partial class Model : ISerializable, IXmlSerializable, IDeserializationCallback { #region Deserialization constructor /// /// Deserialization constructor /// /// The info. /// The context. protected Model(SerializationInfo info, StreamingContext context) { if (Tracing.BinaryDeserializationSwitch.Enabled) { Trace.WriteLine("Deserializing the fields of 'Model'."); } double version = info.GetDouble("ModelVersion"); mPages = info.GetValue( "Pages", typeof(CollectionBase)) as CollectionBase; //Init(); } #endregion #region Serialization events /* [OnSerializing] void OnSerializing(StreamingContext context) { Trace.Indent(); Trace.WriteLine("Starting to serializing the 'Model' class..."); } [OnSerialized] void OnSerialized(StreamingContext context) { Trace.WriteLine("...serialization of 'Model' finished"); Trace.Unindent(); } */ #endregion #region Deserialization events [OnDeserializing] void OnDeserializing(StreamingContext context) { if (Tracing.BinaryDeserializationSwitch.Enabled) Trace.WriteLine("Starting deserializing the 'Model' class..."); //the anchors is a temporary collection of Uid's and parent entities to re-connect connectors //to their parent. The serialization process does serialize parenting because you need on deserialization an //instance in order to connect to it. Anchors.Clear(); } [OnDeserialized] void OnDeserialized(StreamingContext context) { if (Tracing.BinaryDeserializationSwitch.Enabled) Trace.WriteLine("...deserialization of 'Model' finished"); } #endregion #region Serialization /// /// Populates a with the data needed to serialize the target object. /// /// The to populate with data. /// The destination (see ) for this serialization. /// The caller does not have the required permission. public void GetObjectData(SerializationInfo info, StreamingContext context) { if (Tracing.BinarySerializationSwitch.Enabled) Trace.WriteLine("Serializing the fields of 'Model'."); info.AddValue("ModelVersion", modelVersion); info.AddValue("Pages", this.Pages, typeof(CollectionBase)); } #endregion #region Xml serialization /// /// This property is reserved, apply the to the class instead. /// /// /// An that describes the XML representation of the object that is produced by the method and consumed by the method. /// public XmlSchema GetSchema() { throw new NotImplementedException("The method or operation is not implemented."); } /// /// Generates an object from its XML representation. /// /// The stream from which the object is deserialized. public void ReadXml(System.Xml.XmlReader reader) { throw new NotImplementedException("The method or operation is not implemented."); } /// /// Converts an object into its XML representation. /// /// The stream to which the object is serialized. public void WriteXml(System.Xml.XmlWriter writer) { throw new NotImplementedException("The method or operation is not implemented."); } #endregion #region IDeserializationCallback Members /// /// Runs when the entire object graph has been deserialized. /// /// The object that initiated the callback. The functionality for this parameter is not currently implemented. public void OnDeserialization(object sender) { if (Tracing.BinaryDeserializationSwitch.Enabled) Trace.WriteLine("IDeserializationCallback of 'Model' called."); Init(); #region Binding of connectors Dictionary.Enumerator enumer = Anchors.GetEnumerator(); System.Collections.Generic.KeyValuePair pair; Anchor anchor; while (enumer.MoveNext()) { pair = enumer.Current; anchor = pair.Value; if (anchor.Parent != Guid.Empty) //there's a parent connector { if (Anchors.ContainsKey(anchor.Parent)) { Anchors.GetAnchor(anchor.Parent).Instance.AttachConnector(anchor.Instance); } } } //clean up the anchoring matrix Anchors.Clear(); #endregion } #endregion } }