ShereSoft.DeepCloning 1.1.1

dotnet add package ShereSoft.DeepCloning --version 1.1.1
NuGet\Install-Package ShereSoft.DeepCloning -Version 1.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ShereSoft.DeepCloning" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ShereSoft.DeepCloning --version 1.1.1
#r "nuget: ShereSoft.DeepCloning, 1.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.1

// Install ShereSoft.DeepCloning as a Cake Tool
#tool nuget:?package=ShereSoft.DeepCloning&version=1.1.1
  • 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.1 2,680 10/3/2022
1.1.0 617 3/7/2022
1.0.2 420 3/4/2022

1.1.1
* Fixed cloning issues with objects that have custom MemberwiseClone method overloads

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