I am using MemoryStream as deep cloning in one of my methods. I call that method a few times, and I notice the more I call it the more it slows my program. Is there a way to clear the memory stream each time, when I stop using the memory stream?
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}