Skip to main content

Dolly by Peter Andersson

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Dolly

Clone .net objects using source generation

Author: Peter Andersson

NuGet: https://www.nuget.org/packages/Dolly/

You can find more details at https://github.com/AnderssonPeter/Dolly

Source: https://github.com/AnderssonPeter/Dolly

Original Readme

note

Dolly

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

MethodMeanErrorStdDevRatioRatioSDGen0Gen1Allocated
Dolly124.5 ns1.59 ns1.49 ns1.000.020.0362-608 B
DeepCloner457.7 ns7.01 ns6.56 ns3.680.070.0830-1392 B
CloneExtensions566.2 ns9.61 ns8.52 ns4.550.080.0896-1504 B
NClone4,308.0 ns62.01 ns58.01 ns34.610.610.51120.00768584 B
FastCloner15,310.6 ns221.85 ns207.52 ns123.002.160.3967-6800 B
AnyClone19,011.9 ns354.27 ns347.94 ns152.743.252.4414-41256 B

About

note

Clone objects with ease.

How to use

Example (source csproj, source files)

This is the CSharp Project that references Dolly

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dolly" Version="0.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

using System;

namespace Dolly
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class ClonableAttribute : Attribute
{
}
}

Useful

Download Example (.NET C#)

Share Dolly

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dolly

aaa

Category "Clone" has the following generators:

1 CopyTo

2 Dolly

See category

Clone