Baksteen.Extensions.DeepCopy 1.0.0

.NET 6.0
Install-Package Baksteen.Extensions.DeepCopy -Version 1.0.0
dotnet add package Baksteen.Extensions.DeepCopy --version 1.0.0
<PackageReference Include="Baksteen.Extensions.DeepCopy" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Baksteen.Extensions.DeepCopy --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Baksteen.Extensions.DeepCopy, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Baksteen.Extensions.DeepCopy as a Cake Addin
#addin nuget:?package=Baksteen.Extensions.DeepCopy&version=1.0.0

// Install Baksteen.Extensions.DeepCopy as a Cake Tool
#tool nuget:?package=Baksteen.Extensions.DeepCopy&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

C# extension method for fast object cloning.

This is a speed-optimized fork of Alexey Burtsev's deep copier. Depending on your usecase, this will be 2x - 3x faster than the original. It also fixes some bugs which are present in the original code. Compared to the classic binary serialization/deserialization deep clone technique, this version is about seven times faster (the more arrays your objects contain, the bigger the speedup factor).

The speedup is achieved via the following techniques:

  • object reflection results are cached
  • don't deep copy primitives or immutable structs & classes (e.g. enum and string)
  • to improve locality of reference, process the 'fast' dimensions or multidimensional arrays in the inner loops
  • use a compiled lamba expression to call MemberwiseClone

How to use:

using Baksteen.Extensions.DeepCopy;
...
var myobject = new SomeClass();
...
var myclone = myobject.DeepCopy()!;    // creates a new deep copy of the original object 

Note: the exclamation mark (null-forgiving operator) is only required if you enabled nullable referency types in your project

Contributors:

  • Alexey Burtsev (original deep copy code)
  • Wouter Groeneveld (unit tests & XElement copy)
  • Gitkarst (treat enum as immutable)
  • Jean-Paul Mikkers (speed optimization)
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 87 3/18/2022

Initial release