AnderssonPeter / Dolly

Clone .net objects using Source Generators

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Method Mean Error StdDev Ratio RatioSD Gen0 Gen1 Allocated
Dolly 124.5 ns 1.59 ns 1.49 ns 1.00 0.02 0.0362 - 608 B
DeepCloner 457.7 ns 7.01 ns 6.56 ns 3.68 0.07 0.0830 - 1392 B
CloneExtensions 566.2 ns 9.61 ns 8.52 ns 4.55 0.08 0.0896 - 1504 B
NClone 4,308.0 ns 62.01 ns 58.01 ns 34.61 0.61 0.5112 0.0076 8584 B
FastCloner 15,310.6 ns 221.85 ns 207.52 ns 123.00 2.16 0.3967 - 6800 B
AnyClone 19,011.9 ns 354.27 ns 347.94 ns 152.74 3.25 2.4414 - 41256 B

About

Clone .net objects using Source Generators

License:MIT License


Languages

Language:C# 100.0%