CommonNetFuncs.DeepClone
5.0.0
dotnet add package CommonNetFuncs.DeepClone --version 5.0.0
NuGet\Install-Package CommonNetFuncs.DeepClone -Version 5.0.0
<PackageReference Include="CommonNetFuncs.DeepClone" Version="5.0.0" />
<PackageVersion Include="CommonNetFuncs.DeepClone" Version="5.0.0" />
<PackageReference Include="CommonNetFuncs.DeepClone" />
paket add CommonNetFuncs.DeepClone --version 5.0.0
#r "nuget: CommonNetFuncs.DeepClone, 5.0.0"
#:package CommonNetFuncs.DeepClone@5.0.0
#addin nuget:?package=CommonNetFuncs.DeepClone&version=5.0.0
#tool nuget:?package=CommonNetFuncs.DeepClone&version=5.0.0
CommonNetFuncs.DeepClone
Expression-tree graph cloning for net11.0 and netstandard2.1. The package exposes one operation, ExpressionTrees.DeepClone<T>, plus a cache-management property.
The package does not currently contain reflection- or serialization-based cloning methods; DeepCloneR and DeepCloneS do not exist.
Contents
Installation
dotnet add package CommonNetFuncs.DeepClone
using CommonNetFuncs.Core;
using CommonNetFuncs.DeepClone;
using System.Runtime.CompilerServices;
DeepClone<T>
public sealed class Person
{
public string Name { get; set; } = string.Empty;
public Person? Manager { get; set; }
public List<string> Tags { get; set; } = [];
}
Person original = new() { Name = "Chris", Tags = ["staff"] };
original.Manager = original; // A cycle is supported.
Person? clone = original.DeepClone(); // cached mapper
Person? uncached = original.DeepClone(useCache: false); // no shared cache
Person? fromNull = ((Person?)null).DeepClone(); // null
The generic method has one public signature:
sealed class IdentityComparer : EqualityComparer<object>
{
public override bool Equals(object? x, object? y) =>
ReferenceEquals(x, y);
public override int GetHashCode(object obj) =>
RuntimeHelpers.GetHashCode(obj);
}
Dictionary<object, object> references = new(new IdentityComparer());
Person? trackedClone = original.DeepClone(
copiedReferencesDict: references,
useCache: true);
The nullable annotation guarantees a non-null result when the source is non-null. A default or custom dictionary may be supplied; it is populated with original-to-copy mappings and must use reference equality for correct identity behavior. The default dictionary does this. Do not share a dictionary across concurrent operations.
The copier starts with MemberwiseClone, then recursively copies reference-bearing fields. It preserves aliases and cycles within one call, copies arrays (including multidimensional arrays), collections backed by fields, nested structs containing references, and private/readonly fields. Primitive values, enums, strings, and simple structs are reused by value. Delegate-valued fields are set to null; passing a delegate as the top-level object throws ArgumentException.
This is not guaranteed to clone arbitrary runtime resources safely. Streams, native handles, synchronization primitives, proxies, weak references, and framework objects with hidden invariants may remain unusable or retain internal state despite the new outer object. It is also not a serializer, access control boundary, or DTO projection. A clone retains secrets present in the source.
Cache
Compiled copier delegates are cached by Type. useCache: false compiles without adding to the shared cache. The exposed cache is a Core ICacheManagerApi<Type, Func<object, Dictionary<object, object>, object>>.
ExpressionTrees.CacheManager.ClearAllCaches();
ExpressionTrees.CacheManager.SetLimitedCacheSize(128);
ExpressionTrees.CacheManager.SetUseLimitedCache(true);
bool limited = ExpressionTrees.CacheManager.IsUsingLimitedCache();
int capacity = ExpressionTrees.CacheManager.GetLimitedCacheSize();
IReadOnlyDictionary<
Type,
Func<object, Dictionary<object, object>, object>> current =
ExpressionTrees.CacheManager.GetCache();
IReadOnlyDictionary<
Type,
Func<object, Dictionary<object, object>, object>?> limitedCurrent =
ExpressionTrees.CacheManager.GetLimitedCache();
ExpressionTrees.CacheManager.ClearCache();
ExpressionTrees.CacheManager.ClearLimitedCache();
Func<object, Dictionary<object, object>, object> copier =
ExpressionTrees.CacheManager.GetOrAddCache(
typeof(Person),
_ => static (source, references) => source);
bool addedUnlimited =
ExpressionTrees.CacheManager.TryAddCache(typeof(List<int>), copier);
Func<object, Dictionary<object, object>, object> limitedCopier =
ExpressionTrees.CacheManager.GetOrAddLimitedCache(
typeof(string), _ => copier);
bool addedLimited =
ExpressionTrees.CacheManager.TryAddLimitedCache(
typeof(Dictionary<int, int>), copier);
ExpressionTrees.CacheManager.ClearAllCaches(); // Remove the demonstration delegates.
CacheManager is configurable and thread-safe, but the call-scoped memo is thread-local. The cache can grow with the number of distinct runtime types; clear or limit it when cloning many dynamically generated types.
License
MIT. See LICENSE.
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CommonNetFuncs.Core (>= 5.0.0)
-
net11.0
- CommonNetFuncs.Core (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CommonNetFuncs.DeepClone:
| Package | Downloads |
|---|---|
|
CommonNetFuncs.Web.Api
Generic API endpoints for use in an ASP.NET API |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 5.0.0 | 0 | 11/23/2026 |
| 4.4.7 | 121 | 11/11/2026 |
| 4.4.6 | 173 | 10/29/2026 |
| 4.4.5 | 117 | 10/27/2026 |
| 4.4.4 | 129 | 10/22/2026 |
| 4.4.3 | 143 | 10/15/2026 |
| 4.4.2 | 129 | 10/10/2026 |
| 4.4.1 | 114 | 10/10/2026 |
| 4.4.0 | 112 | 10/9/2026 |
| 4.3.6 | 243 | 9/15/2026 |
| 4.3.5 | 154 | 9/1/2026 |
| 4.3.4 | 172 | 8/27/2026 |
| 4.3.3 | 153 | 8/24/2026 |
| 4.3.2 | 144 | 8/14/2026 |
| 4.3.1 | 139 | 8/11/2026 |
| 4.3.0 | 170 | 8/3/2026 |
| 4.1.14 | 137 | 8/3/2026 |
| 4.1.12 | 151 | 7/31/2026 |
| 4.1.11 | 195 | 7/9/2026 |
| 4.1.0 | 274 | 6/5/2026 |