FiE-Game/Assets/Scripts/Fie/Utility/FieDeepClone.cs

22 lines
499 B
C#
Raw Normal View History

2018-11-20 20:10:49 +01:00
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Fie.Utility
{
public static class FieDeepClone
{
public static T CloneDeep<T>(this T target)
{
object obj = null;
using (MemoryStream memoryStream = new MemoryStream())
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, target);
memoryStream.Position = 0L;
obj = binaryFormatter.Deserialize(memoryStream);
}
return (T)obj;
}
}
}