RSCG – NanoByte.CloneGenerator
| name | NanoByte.CloneGenerator |
| nuget | https://www.nuget.org/packages/NanoByte.CloneGenerator/ |
| link | |
| author |
Cloning objects
This is how you can use NanoByte.CloneGenerator .
The code that you start with is
<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>
The code that you will use is
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());
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; } = [];
}
The code that is generated is
// <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; }
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/NanoByte.CloneGenerator