public static List<TEntity> Clone<TEntity>(List<TEntity> original)
{
List<TEntity> returnValue = null;
using (var stream = new System.IO.MemoryStream())
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, original);
stream.Position = 0;
returnValue = binaryFormatter.Deserialize(stream) as List<TEntity>;
}
return returnValue;
}
You can make your method even more general by allowing any type to not only List<>, see my answer to the same question with a set of unit tests, error handling, and it is also implemented as an extension method that is so easy to use. See fooobar.com/questions/2367 / ...
Method Signature:
public static TObject DeepCopy<TObject>(
this TObject instance,
bool throwInCaseOfError)
where TObject : class
Perhaps you can create a simpler overload without a parameter throwInCaseOfError:
public static TObject DeepCopy<TObject>(this TObject instance)
where TObject : class