AnyClone 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AnyClone --version 1.0.0
NuGet\Install-Package AnyClone -Version 1.0.0
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="AnyClone" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AnyClone --version 1.0.0
#r "nuget: AnyClone, 1.0.0"
#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 AnyClone as a Cake Addin
#addin nuget:?package=AnyClone&version=1.0.0

// Install AnyClone as a Cake Tool
#tool nuget:?package=AnyClone&version=1.0.0

AnyClone

A CSharp library that can deep clone any object using only reflection.

No requirements for [Serializable] attributes, supports standard ignore attributes.

Description

I built this library as almost all others I tried on complex objects either didn't work at all, or failed to account for common scenarios. Serialization required too much boiler plate (BinarySerialization, Protobuf, or Json.Net) and fails to account for various designs. Implementing IClonable was too much of a chore and should be unnecessary. Various projects that use expression trees also failed to work, IObservable patterns were difficult to implement on large, already written code base.

Usage

var originalObject = new SomeComplexTypeWithDeepStructure();
var myClonedObject = originalObject.Clone();

Capture Errors

var originalObject = new SomeComplexTypeWithDeepStructure();
// capture errors found with your object where impossible situations occur, and add [IgnoreDataMember] to those properties/fields.

var myClonedObject = originalObject.Clone((ex, path, property, obj) => {
  Console.WriteLine($"Cloning error: {path} {ex.Message}");
  Assert.Fail();
});

Get differences between cloned objects using AnyDiff

// using AnyDiff;
var object1 = new MyComplexObject(1, "A string");
var object1Snapshot = object1.Clone();

var diff = AnyDiff.Diff(object1, object1Snapshot);
Assert.AreEqual(diff.Count, 0);

// change something anywhere in the object tree
object1.Name = "A different string";

diff = AnyDiff.Diff(object1, object1Snapshot);
Assert.AreEqual(diff.Count, 1);

Ignoring Properties/Fields

There are unfortunately a few situations that can't be resolved, such as cloning delegates, events etc. Fortunately, and for most scenarios you don't want these anyways so you can use any of the standard supported attributes to ignore these properties: [IgnoreDataMember], [JsonIgnore], and [NonSerialized] (fields only, just use [IgnoreDataMember] and save yourself the hassle).

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on AnyClone:

Package Downloads
Blazor-State

A Blazor state management library by TimeWarp

Core-State

A fork of blazor-state. Core State is a MediatR pipeline for Blazor, Xamarin, UWP, WPF state management

IdOps.Server

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on AnyClone:

Repository Stars
TimeWarpEngineering/blazor-state
A Blazor State management library by TimeWarp.
Version Downloads Last updated
1.1.6 123,874 6/20/2022
1.1.3 414 6/20/2022
1.1.2 36,903 9/15/2021
1.1.1 380 9/15/2021
1.1.0 990 9/15/2021
1.0.68 1,912 8/18/2021
1.0.67 354 8/11/2021
1.0.65 334 8/10/2021
1.0.64 331 8/10/2021
1.0.63 2,333 7/29/2021
1.0.62 356 7/29/2021
1.0.61 399 7/29/2021
1.0.57 399 7/28/2021
1.0.56 378 7/28/2021
1.0.55 411 7/28/2021
1.0.39 93,002 7/10/2020
1.0.37 576 7/9/2020
1.0.36 38,313 4/22/2020
1.0.35 465 4/22/2020
1.0.34 18,762 5/16/2019
1.0.31 1,069 4/20/2019
1.0.30 906 4/20/2019
1.0.29 929 4/20/2019
1.0.27 916 4/14/2019
1.0.24 2,503 1/11/2019
1.0.23 43,360 1/7/2019
1.0.22 990 12/29/2018
1.0.17 998 12/28/2018
1.0.11 12,761 12/4/2018
1.0.10 1,012 12/4/2018
1.0.9 1,023 12/2/2018
1.0.8 1,000 12/2/2018
1.0.6 1,005 11/29/2018
1.0.5 998 11/29/2018
1.0.4 1,049 11/28/2018
1.0.0 1,135 11/16/2018

Deep clone or copy any complex object without the hassle of serialization, decoration or custom mappings.