Dolly by Peter Andersson
Nuget / site data
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
Original Readme
note
Dolly
Clone .net objects using source generation
·Report Bug·Request Feature·
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 aspartial
. - Add
[CloneIgnore]
to any property or field that you don't want to include in the clone. - Call
DeepClone()
orShallowClone()
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
note
Clone objects with ease.
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
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>
This is the use of Dolly in Program.cs
// See https://aka.ms/new-console-template for more information
using CloneData;
Console.WriteLine("Hello, World!");
Person p = new ();
p.FirstName = "Andrei";
p.LastName = "Ignat";
p.Age = 54;
var p1=p.DeepClone();
Console.WriteLine(p1.Name());
This is the use of Dolly in Person.cs
namespace CloneData;
[Dolly.Clonable]
public partial class Person
{
public string FirstName { get; set; } = "";
public string LastName { get; set; } = "";
[Dolly.CloneIgnore]
public int Age { get; set; }
public string Name() => $"{FirstName} {LastName}";
public Person[] Childs { get; set; } = [];
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- ClonableAttribute.g.cs
- CloneIgnoreAttribute.g.cs
- IClonable.g.cs
- Person.g.cs
using System;
namespace Dolly
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class ClonableAttribute : Attribute
{
}
}
using System;
namespace Dolly
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class CloneIgnoreAttribute : Attribute
{
}
}
using System;
namespace Dolly
{
public interface IClonable<T> : ICloneable
{
T DeepClone();
T ShallowClone();
}
}
using Dolly;
using System.Linq;
namespace CloneData;
partial class Person : IClonable<Person>
{
object ICloneable.Clone() => this.DeepClone();
public virtual Person DeepClone() =>
new ()
{
FirstName = FirstName,
LastName = LastName,
Childs = Childs.Select(item => item.DeepClone()).ToArray()
};
public virtual Person ShallowClone() =>
new ()
{
FirstName = FirstName,
LastName = LastName,
Childs = Childs.ToArray()
};
}
Usefull
Download Example (.NET C# )
Share Dolly
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dolly