I need to clone lists in different parts of the system I'm working on and I came up with the below solution, but I'm still concerned about it's performance I've been wondering if that is a good method at all. So I decided to post it here so that I can find if there is any problem or flaw related to this method of cloning objects. And if it is a bad move, I'd like to know what is the best approach.
My extension method for cloning a list:
public static List<T> Clone<T>(this List<T> input)
{
using (System.IO.MemoryStream Mems = new System.IO.MemoryStream())
{
XmlSerializer Serializer = new XmlSerializer(typeof(T));
Serializer.Serialize(Mems, input);
var Deserialized = Serializer.Deserialize(Mems) as List<T>;
return Deserialized;
}
}
Update: Assuming T is a value type (Always)