README.md

November 16, 2025 · View on GitHub

Logo

Dolly

Clone .net objects using source generation

· Report Bug · Request Feature ·


NuGet version Nuget GitHub license

unit tests Testspace tests Coverage Status

Table of Contents

About The Project

Generate c# code to clone objects on the fly.

Getting Started

  • Add the Dolly nuget and add [Clonable] attribute to a class and ensure that the class is marked as partial.
  • Add [CloneIgnore] to any property or field that you don't want to include in the clone.
  • Call DeepClone() or ShallowClone() on the object.

Example

[Clonable]
public partial class SimpleClass
{
    public string First { get; set; }
    public int Second { get; set; }
    [CloneIgnore]
    public float DontClone { get; set; }
}

Should generate

partial class SimpleClass : IClonable<SimpleClass>
{
    
    object ICloneable.Clone() => this.DeepClone();

    public SimpleClass DeepClone() =>
        new SimpleClass()
        {
            First = First,
            Second = Second
        };

    public SimpleClass ShallowClone() =>
        new SimpleClass()
        {
            First = First,
            Second = Second
        };
}

Benchmarks

MethodMeanErrorStdDevMedianRatioRatioSDGen0AllocatedAlloc Ratio
FastCloner.SourceGenerator64.48 ns1.323 ns3.145 ns64.11 ns0.460.030.0094472 B0.78
Dolly140.85 ns2.835 ns7.165 ns139.39 ns1.000.070.0119608 B1.00
DeepCloner501.48 ns13.753 ns37.879 ns490.50 ns3.570.320.02771392 B2.29
CloneExtensions694.84 ns11.787 ns11.576 ns694.27 ns4.940.250.02961504 B2.47
NClone4,970.65 ns91.412 ns245.572 ns4,948.11 ns35.372.410.16788584 B14.12
FastDeepCloner17,968.60 ns356.017 ns462.923 ns17,988.80 ns127.886.860.12216944 B11.42
AnyClone22,256.06 ns443.712 ns1,001.532 ns22,134.10 ns158.3910.310.793541256 B67.86