Add a deep-clone func to the network. We will end up using this for adding ctrl-z functionality.

This commit is contained in:
Tim Young 2018-03-15 16:12:00 -05:00
parent 8d1ba61b89
commit 6593a172fd

View File

@ -10,6 +10,9 @@ using System.IO;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace EduNetworkBuilder
@ -17,6 +20,7 @@ namespace EduNetworkBuilder
/// <summary>
/// This is a whole network. LAN, WAN, Internet; everything combined
/// </summary>
[Serializable]
public class Network
{
public string PuzzleName = "";
@ -150,6 +154,30 @@ namespace EduNetworkBuilder
return newitem;
}
//from: http://stackoverflow.com/questions/78536/cloning-objects-in-c
public static T DeepClone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
public void ClearComponents()
{
NetComponents.Clear();