Skip to content

Commit

Permalink
(infra) Implement deep_copy
Browse files Browse the repository at this point in the history
This allows copying a complex object over so that changes can be made
without affecting the original object.
  • Loading branch information
ferventcoder committed Feb 4, 2015
1 parent 17e259a commit 8970a84
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/chocolatey/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace chocolatey
{
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
/// Extensions for Object
/// </summary>
Expand All @@ -31,5 +34,17 @@ public static string to_string(this object input)

return input.ToString();
}

public static T deep_copy<T>(this T other)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, other);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}

}
}

0 comments on commit 8970a84

Please sign in to comment.