Advertisement
_takumi

SaveLoad.cs

Dec 13th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.  
  8. namespace PolygonDraft
  9. {
  10.     public interface IOriginator
  11.     {
  12.         IMemento CreateMemento();
  13.     }
  14.  
  15.     public interface IMemento
  16.     {
  17.         ExpandoObject GetState();
  18.     }
  19.  
  20.     public static class SaveOpenManager
  21.     {
  22.         private static BinaryFormatter _binFormatter = new BinaryFormatter();
  23.         public static void Save(string path, FileMode mode, IMemento memento)
  24.         {
  25.             using (FileStream stream = new FileStream(path, mode))
  26.             {
  27.                 _binFormatter.Serialize(stream, memento);
  28.             }
  29.         }
  30.         public static IMemento Load(string path, FileMode mode)
  31.         {
  32.             IMemento memento;
  33.             using (FileStream stream = new FileStream(path, mode))
  34.             {
  35.                 memento = (IMemento)_binFormatter.Deserialize(stream);
  36.             }
  37.             return memento;
  38.         }
  39.     }
  40.  
  41.     [Serializable]
  42.     public class MCMemento : IMemento
  43.     {
  44.         private readonly Polygon _polygon;
  45.         private readonly Color _backColor;
  46.         private readonly Type _type;
  47.         private readonly int _index;
  48.         private readonly List<Type> _pluggedInTypes;
  49.  
  50.         public MCMemento(Polygon polygon, Color backColor,
  51.             Type type, int index, List<Type> pluggedInTypes)
  52.         {
  53.             this._polygon = polygon;
  54.             this._backColor = backColor;
  55.             this._type = type;
  56.             this._index = index;
  57.             this._pluggedInTypes = pluggedInTypes;
  58.         }
  59.  
  60.         public ExpandoObject GetState()
  61.         {
  62.             dynamic state = new ExpandoObject();
  63.             state.Polygon = _polygon;
  64.             state.Type = _type;
  65.             state.BackColor = _backColor;
  66.             state.Index = _index;
  67.             state.PluggedInTypes = _pluggedInTypes;
  68.             return state;
  69.         }
  70.  
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement