Skip to main content

NanoByte.CloneGenerator by

NuGet / site data​

Nuget

Details​

Info​

info

Name: NanoByte.CloneGenerator

Roslyn source generator for deep Clone() methods. Annotate a partial class with [Cloneable] and the member-by-member copy is generated at compile time.

Author:

NuGet: https://www.nuget.org/packages/NanoByte.CloneGenerator/

You can find more details at

Source:

Author​

note

Original Readme​

note

About​

note

Cloning objects

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references NanoByte.CloneGenerator

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NanoByte.CloneGenerator" Version="0.2.1" PrivateAssets="All"/>
</ItemGroup>

</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated/>
#nullable enable
#pragma warning disable CS0618 // Member is obsolete
#pragma warning disable CS0612 // Member is obsolete

namespace CloneData
{
partial class Person
{
/// <summary>Copies all members declared by <see cref="Person"/> from one instance to another.</summary>
protected static void CloneFromTo(global::CloneData.Person from, global::CloneData.Person to)
{
if (from == null) throw new global::System.ArgumentNullException(nameof(from));
if (to == null) throw new global::System.ArgumentNullException(nameof(to));
to.FirstName = from.FirstName;
to.LastName = from.LastName;
to.Age = from.Age;
to.Childs = from.Childs;
}

/// <summary>Creates a deep copy of this <see cref="Person"/> instance.</summary>
/// <returns>The new copy of the <see cref="Person"/>.</returns>
public virtual global::CloneData.Person Clone()
{
var from = this;
var to = new global::CloneData.Person();
CloneFromTo(from, to);
return to;
}
}
}

Useful​

Download Example (.NET C#)​

Share NanoByte.CloneGenerator​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/NanoByte.CloneGenerator

Category "Clone" has the following generators:​

1 CopyTo Nuget GitHub Repo stars 2024-02-19

2 Dolly Nuget GitHub Repo stars 2024-12-03

3 FastCloner Nuget GitHub Repo stars 2026-02-01

4 NanoByte.CloneGenerator Nuget 2026-10-02

5 UtilityVerse.Copy Nuget GitHub Repo stars 2025-08-15

See category​

Clone