NanoByte.CloneGenerator by
NuGet / site data
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)
- CSharp Project
- Program.cs
- Person.cs
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>
This is the use of NanoByte.CloneGenerator in Program.cs
using CloneData;
Console.WriteLine("Hello, World!");
Person p = new ();
p.FirstName = "Andrei";
p.LastName = "Ignat";
p.Age = 54;
var p1=p.Clone();
Console.WriteLine(p1.Name());
This is the use of NanoByte.CloneGenerator in Person.cs
using NanoByte.CloneGenerator;
namespace CloneData;
[Cloneable]
public partial class Person
{
public string FirstName \{ get; set; \} = "";
public string LastName \{ get; set; \} = "";
public int Age \{ get; set; }
public string Name() => $"{FirstName} {LastName}";
public Person[] Childs \{ get; set; \} = [];
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- CloneData.Person.Clone.g.cs
- CloneGeneratorAttributes.g.cs
// <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;
}
}
}
// <auto-generated/>
#pragma warning disable
namespace NanoByte.CloneGenerator
{
// Generates a deep Clone() method for this type. The type must be partial.
[global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = false)]
[global::System.CodeDom.Compiler.GeneratedCode("NanoByte.CloneGenerator", null)]
internal sealed class CloneableAttribute : global::System.Attribute
{
// The name of the generated method returning the concrete type. Only used when a base
// type already declares a Clone() method with a different return type.
// Defaults to "Clone" + TypeName.
public string MethodName \{ get; set; }
}
// Excludes this member from the generated clone. The clone keeps the member's default value.
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field, Inherited = false)]
[global::System.CodeDom.Compiler.GeneratedCode("NanoByte.CloneGenerator", null)]
internal sealed class IgnoreCloneAttribute : global::System.Attribute
{}
// Copies this member by reference, even if its type is cloneable. The clone shares the instance.
[global::System.AttributeUsage(global::System.AttributeTargets.Property | global::System.AttributeTargets.Field, Inherited = false)]
[global::System.CodeDom.Compiler.GeneratedCode("NanoByte.CloneGenerator", null)]
internal sealed class ShallowCloneAttribute : global::System.Attribute
{}
// Declares a type as safe to copy by reference, typically because it is immutable.
// Applies to every member of that type in the assembly.
[global::System.AttributeUsage(global::System.AttributeTargets.Assembly, AllowMultiple = true)]
[global::System.CodeDom.Compiler.GeneratedCode("NanoByte.CloneGenerator", null)]
internal sealed class CloneShallowAttribute : global::System.Attribute
{
public CloneShallowAttribute(global::System.Type type) \{ Type = type; }
public global::System.Type Type \{ get; }
}
}
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
2024-02-19
2 Dolly
2024-12-03
3 FastCloner
2026-02-01
4 NanoByte.CloneGenerator 2026-10-02
5 UtilityVerse.Copy
2025-08-15