I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone()
.
Is there an easy way around this?
I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone()
.
Is there an easy way around this?
To clone a list just call .ToList()
If you have already referenced Newtonsoft.Json in your project and your objects are serializeable you could always use:
Possibly not the most efficient way to do it, but unless you're doing it 100s of 1000s of times you may not even notice the speed difference.
I use automapper to copy an object. I just setup a mapping that maps one object to itself. You can wrap this operation any way you like.
http://automapper.codeplex.com/
This is one way to do it with C# and .NET 2.0. Your object requires to be
[Serializable()]
. The goal is to lose all references and build new ones.I've made for my own some extension which converts ICollection of items that not implement IClonable