FastCloner 3.3.8

dotnet add package FastCloner --version 3.3.8
                    
NuGet\Install-Package FastCloner -Version 3.3.8
                    
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="FastCloner" Version="3.3.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastCloner" Version="3.3.8" />
                    
Directory.Packages.props
<PackageReference Include="FastCloner" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FastCloner --version 3.3.8
                    
#r "nuget: FastCloner, 3.3.8"
                    
#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.
#addin nuget:?package=FastCloner&version=3.3.8
                    
Install FastCloner as a Cake Addin
#tool nuget:?package=FastCloner&version=3.3.8
                    
Install FastCloner as a Cake Tool

FastCloner FastCloner.Contrib

FastCloner

<img align="left" width="128" height="128" alt="Te Reo Icon" src="https://github.com/user-attachments/assets/54f5be37-543a-411d-b6e6-90a77414926c" /> Fast deep cloning library, supporting anything from <code>.NET 4.6</code> to modern <code>.NET 8+</code>. Supports both deep and shallow cloning. Extensively tested, focused on performance and stability even on complicated object graphs. FastCloner is designed to work with as few gotchas as possible out of the box. The mapping is zero-config by default. Clone your objects and be done with it <em>fast</em>. FastCloner builds upon <a href="https://github.com/force-net/DeepCloner">DeepClone</a>.

<br/><br/>

Getting Started

Install the package via NuGet:

dotnet add package FastCloner
dotnet add package FastCloner.Contrib # only required for some special types, such as Fonts

Clone your objects:

using FastCloner.Code;
var clone = FastCloner.FastCloner.DeepClone(new { Hello = "world", MyList = new List<int> { 1 } });

That's it! For convenience, please add the following method to your project. We intentionally don't ship this extension to make switching from/to FastCloner easier:

[return: NotNullIfNotNull(nameof(obj))]
public static T? DeepClone<T>(this T? obj)
{
    return FastCloner.FastCloner.DeepClone(obj);
}

Advanced usage

The following examples assume you've copied the extension method above.

Sometimes, you might want to exclude certain fields (including event synthesized) and properties from cloning:

private class TestPropsWithIgnored
{
    [FastClonerIgnore] // <-- decorate with [FastClonerIgnore]
    public string B { get; set; } = "My string";

    public int A { get; set; } = 10;
}

TestPropsWithIgnored original = new TestPropsWithIgnored { A = 42, B = "Test value" };
TestPropsWithIgnored clone = original.DeepClone(); // clone.B is null (default value of a given type)

You might also need to exclude certain types from being cloned ever. To do that, put offending types on a blacklist:

FastCloner.FastCloner.IgnoreType(typeof(PropertyChangedEventHandler)); // or FastCloner.FastCloner.IgnoreTypes([ .. ])

If needed, the types can be removed from the blacklist later:

// note: doing this invalidates precompiled expressions and clears the cache,
// performance will be negatively affected until the cache is repopulated
FastCloner.FastCloner.ClearIgnoredTypes();

Apart from deep cloning, FastCloner supports shallow cloning and deep cloning to target:

// the list is shared between the two instances
var clone = FastCloner.FastCloner.ShallowClone(new { Hello = "world", MyList = new List<int> { 1 } });

Limitations

FastCloner uses caching by default, which makes evaluating properties harder. Cloning unmanaged resources, such as IntPtrs may result in side-effects, as there is no metadata for the length of buffers such pointers often point to. ReadOnly and Immutable collections are tested to behave well if they follow basic conventions. Many other features, such as cloning Dictionaryies properly while keeping hashcodes, INotifyPropertyChanged, delegates, events, HttpRequests / responses, and others are supported. If something doesn't work out of the box, let me know in the issues, the repository is actively maintained.

Cache can be invalidated to reduce the memory footprint, if needed:

FastCloner.FastCloner.ClearCache();

Performance

FastCloner aims to work correctly and meet reasonable expectations by default while being fast. Benchmarking results are available here, check them out! By default, FastCloner relies on heavily cached reflection to work. An incremental source generator is currently in development as an opt-in alternative for performance-critical scenarios.

Contributing

If you are looking to add new functionality, please open an issue first to verify your intent is aligned with the scope of the project. The library is covered by ~300 tests, please run them against your work before proposing changes. When reporting issues, providing a minimal reproduction we can plug in as a new test greatly reduces turnaround time.

License

This library is licensed under the MIT license. 💜

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 is compatible.  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. 
.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 net46 is compatible.  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.
  • .NETFramework 4.6

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on FastCloner:

Package Downloads
SPTarkov.Server.Core

Core library for the Single Player Tarkov server.

FastCloner.Contrib

Extends FastCloner with support for certain special types.

FastCloner.Benchmark

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.3.8 41 6/6/2025
3.3.7 46 6/6/2025
3.3.6 131 6/4/2025
3.3.5 111 6/4/2025
3.3.4 4,740 4/22/2025
3.3.3 4,445 3/31/2025
3.3.2 13,718 1/9/2025
3.3.1 67 1/9/2025
3.2.1 67 1/8/2025
3.1.9 85 1/8/2025
3.1.7 76 1/8/2025
3.1.6 71 1/8/2025
3.1.5 71 1/8/2025
3.1.4 93 1/8/2025
1.1.4 81 1/7/2025
0.1.2 75 1/8/2025