ShereSoft.DeepCloning 1.1.0

.NET 5.0 .NET Core 3.1 .NET Framework 4.0
Install-Package ShereSoft.DeepCloning -Version 1.1.0
dotnet add package ShereSoft.DeepCloning --version 1.1.0
<PackageReference Include="ShereSoft.DeepCloning" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ShereSoft.DeepCloning --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ShereSoft.DeepCloning, 1.1.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 ShereSoft.DeepCloning as a Cake Addin
#addin nuget:?package=ShereSoft.DeepCloning&version=1.1.0

// Install ShereSoft.DeepCloning as a Cake Tool
#tool nuget:?package=ShereSoft.DeepCloning&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
  • Can deep-clone instances of any object, including immutables, multi-dimensional arrays and non-public objects
  • Maintains shared references within the instance including any circular references
  • Uses generic cloner types custom-built and compiled at runtime
  • Largely written in Common Intermediate Language (.NET Assembler) for performance (No use of Reflection for cloning)
  • Configurable for string handling (shallow-copy or deep-copy) and singleton handling (try to reuse or deep-copy)
  • Thread-Safe and lock-free
  • Includes extension methods (ShereSoft.Extensions.ObjectExtensions) for conveniently calling from any object instance
  • Unit Tested
  • Multi-targeted (.NET 6, .NET 5, .NET Core 3.1, .NET Framework 4.8, .NET Framework 4.7, .NET Framework 4.6, .NET Framework 4.5, .NET Framework 4.0)
  • No external library dependencies whatsoever

QUICK START

Using the extension methods (included) is recommended.

.DeepClone()

using ShereSoft.Extensions;
:

var customer = new Customer();
:

Customer clone = customer.DeepClone();

DeepCloningOptions

.DeepCloneStrings = true

var customer = new Customer();
customer.Name = "John";

var options = new DeepCloningOptions { DeepCloneStrings = true };
Customer clone = customer.DeepClone(options);

Debug.WriteLine(Object.ReferenceEquals(customer, clone));  // False (Default is reuse, NOT deep copy)

Strings are reused by default as they are normally treated as immutable. If your code contains any string manipulation by pointer, use this option to prevent unwanted side effects to the original object.

.DeepCloneSingletons = true

class Customer
{
    public static readonly Customer Empty = new Customer();  // Static readonly fields as singletons
    :
}

var customer = Customer.Empty;

var options = new DeepCloningOptions { DeepCloneSingletons = true };
Customer clone = customer.DeepClone(options);

Debug.WriteLine(Object.ReferenceEquals(customer, clone));  // False (Default is reuse, NOT deep copy)

"static readonly" fields are assumed to be immutable. If your code is not, which by the way is not the best practice, use this option.

.UnclonableObjects.Add(object)

class Customer
{
    public System.Globalization.CultureInfo UserCulture { get; set; }  // Built-in, read-only CultureInfo object...
    :
}

var customer = new Customer();
:

var options = new DeepCloningOptions();
options.UnclonableObjects.Add(customer.UserCulture);

Customer clone = customer.DeepClone(options);

Debug.WriteLine(Object.ReferenceEquals(customer.UserCulture, clone.UserCulture));  // True

Register any objects that do not need deep-copying in DeepCloningOptions.UnclonableObjects. Shallow-copying also improves performance.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.1
.NET Framework net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • net5.0

    • No dependencies.
  • 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.1.0 113 3/7/2022
1.0.2 67 3/4/2022

1.1.0
* Can optionally specify unclonable objects in DeepCloningOptions.UnclonableObjects (Use this feature to force to shallow-copy)
* Use the native value copy for all simple value types not limited to primitives (Improved simple value type detection)
* Fixed issues with reusing plain object instances (A new instance was created every time. No reusing.)
* Inlined the sealed type detection and the actual/declared type detection as part of the cloner
* Improved startup time

1.0.2
* Fixed issues with copying null elements in collections
* Improved performance by a single delegate, more optimized runtime evaluation, more inlining implementation and more optimized Assembler code
* Added unit tests for the resolved issues
* Reduced the assembly size.

1.0.1
* Added .NET Framework support for 4.0, 4.6, 4.7 and 4.8
* Made cloner creation lock-free

1.0.0
* Can deep-clone instances of any object
* Implemented in Common Intermediate Language (.NET Assembly), compiled for each type at runtime, and cached for subsequent use.
* No use of Reflection for cloning
* Configurable for string handling (deep clone or not) and singleton handling (reuse or clone)
* Thread-safe
* Includes extension methods (ObjectExtensions) for conveniently calling from any object instance (Recommended usage)
* Unit Tested
* Multi-targeted (.NET 6, .NET 5, .NET Core 3.1, .NET Framework 4.5)
* No external library dependencies
* No external calls