public static DiagramSerialize GetSurrogate(object graph, SurrogateSelector selector) { //Retrieve the surrogate reference //Sub classes may have added other surrogates //Start with the type of the deserialized data Type type = graph.GetType().UnderlyingSystemType; ISurrogateSelector iselector = (ISurrogateSelector)selector; DiagramSerialize surrogate = null; //Walk through the types until we find a surrogate while (type != typeof(System.Windows.Forms.UserControl)) { surrogate = (DiagramSerialize)selector.GetSurrogate(type, new StreamingContext(StreamingContextStates.All), out iselector); if (surrogate != null) { break; } type = type.BaseType; //return the base type and try again } return(surrogate); }
private void LoadDiagram(Stream fs, IFormatter formatter) { Diagram diagram; SurrogateSelector selector = new SurrogateSelector(); DiagramSerialize surrogate = new DiagramSerialize(); try { selector.AddSurrogate(typeof(Diagram),new StreamingContext(StreamingContextStates.All), surrogate); //Raise the deserialize event and allow subclasses to add/change the surrogate to their own surrogate OnDeserialize(formatter,selector); formatter.SurrogateSelector = selector; formatter.Binder = Component.Instance.DefaultBinder; diagram = (Diagram) formatter.Deserialize(fs); } catch (Exception ex) { if (ex.InnerException == null) { throw ex; } else { throw ex.InnerException; } } finally { } surrogate = Serialization.Serialize.GetSurrogate(diagram,selector); if (surrogate == null) throw new Exception("A deserialization surrogate could not be found."); //Update this object using the surrogate and the diagram surrogate.UpdateObjectReferences(); SuspendEvents = true; Suspend(); //Copy settings from deserialized object DiagramSize = diagram.DiagramSize; Zoom = diagram.Zoom; ShowTooltips = diagram.ShowTooltips; CheckBounds = diagram.CheckBounds; Paged = diagram.Paged; WorkspaceColor = diagram.WorkspaceColor; //Copy all layers across Layers.Clear(); foreach (Layer layer in surrogate.Layers) { Layers.Add(layer); } Layers.CurrentLayer = surrogate.Layers.CurrentLayer; //Copy shapes and lines accross Shapes.Clear(); foreach (Shape shape in surrogate.Shapes.Values) { Shapes.Add(shape.Key,shape); } Lines.Clear(); foreach (Line line in surrogate.Lines.Values) { Lines.Add(line.Key,line); } mNavigate = new Navigation.Navigate(this); Route = new Route(); Route.Container = this; Margin = new Margin(); //##margin needs to be serialized/deserialized //Raise the deserialize complete event OnDeserializeComplete(diagram, formatter, selector); Resume(); SuspendEvents = false; Refresh(); }