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()
There is a simple way to clone objects in C# using a JSON serializer and deserializer.
You can create an extension class:
using Newtonsoft.Json;
static class typeExtensions
{
[Extension()]
public static T jsonCloneObject(T source)
{
string json = JsonConvert.SerializeObject(source);
return JsonConvert.DeserializeObject(json);
}
}
To clone and object:
obj clonedObj = originalObj.jsonCloneObject;