A lightweight .NET library providing a non-generic interface and type-safe base class for implementing deep cloning functionality in your classes. Supports inheritance hierarchies, circular references, and various collection types including immutable collections. Thread-safe and designed for performance with minimal dependencies.
$ dotnet add package ktsu.DeepCloneA lightweight .NET library providing a simple, generic interface for implementing deep cloning functionality in your classes.
The ktsu.DeepClone library defines the IDeepCloneable<T> interface, which allows you to create deep copies of objects. This is particularly useful in scenarios where you need to duplicate an object while ensuring that its references to other objects are also fully cloned, not just copied.
Inspired by and based on the ppy/osu! project's cloning utilities, this library is licensed under the MIT License. See the LICENSE file for more details.
class).Install the library via NuGet (coming soon):
dotnet add package ktsu.DeepClone
Or, clone the repository and include the source code directly in your project.
To use ktsu.DeepClone, implement the IDeepCloneable<T> interface in your class:
using ktsu.DeepClone;
public class MyClass : IDeepCloneable<MyClass>
{
public int Value { get; set; }
public MyClass NestedObject { get; set; }
public MyClass DeepClone()
{
return new MyClass
{
Value = this.Value,
NestedObject = this.NestedObject?.DeepClone()
};
}
}
You can then create deep copies of your objects:
var original = new MyClass
{
Value = 42,
NestedObject = new MyClass { Value = 84 }
};
var copy = original.DeepClone();
// The copy is a completely independent object
copy.Value = 100;
copy.NestedObject.Value = 200;
// Original remains unchanged
Console.WriteLine(original.Value); // Outputs: 42
Console.WriteLine(original.NestedObject.Value); // Outputs: 84
Contributions are welcome! If you have suggestions or feature requests, please feel free to open an issue or submit a pull request.
git clone https://github.com/ktsu-dev/ktsu.DeepClone.git
This library is licensed under the MIT License. See the LICENSE file for details.
This library is inspired by the ppy/osu! project's cloning utilities. Many thanks to their team for their foundational work and open-source contributions.