RSCG – FastCloner
| name | FastCloner |
| nuget | https://www.nuget.org/packages/FastCloner/ |
| link | https://github.com/lofcz/FastCloner/ |
| author | Matěj Štágl |
Cloning objects
This is how you can use FastCloner .
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="FastCloner" Version="3.4.4" />
<PackageReference Include="FastCloner.SourceGenerator" Version="1.1.4" />
</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.FastDeepClone();
Console.WriteLine(p1.Name());
using FastCloner.SourceGenerator.Shared;
namespace CloneData;
[FastClonerClonable]
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
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()
};
}
//
// Generated: 2026-02-03 05:29:59 UTC
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using FastCloner.SourceGenerator.Shared;
using FastCloner;
namespace CloneData
{
///
///
public static partial class PersonFastDeepCloneExtensions
{
///
///
/// The object to clone.
[return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(“source”)]
public static global::CloneData.Person? FastDeepClone(this global::CloneData.Person? source)
{
return InternalFastDeepClone(source, null);
}
///
///
/// The object to clone.
/// State for circular reference tracking. If null, a new state is created.
internal static global::CloneData.Person? InternalFastDeepClone(this global::CloneData.Person? source, FcGeneratedCloneState? state)
{
if (source == null) return null;
var localState = state ?? new FcGeneratedCloneState();
var known = localState.GetKnownRef(source);
if (known != null) return (global::CloneData.Person)known;
var result = new global::CloneData.Person();
localState?.AddKnownRef(source, result);
result.FirstName = source.FirstName;
result.LastName = source.LastName;
result.Age = source.Age;
result.Childs = FastClonerSgCloneCloneData_Person__(source.Childs, localState);
return result;
}
private static global::CloneData.Person[]? FastClonerSgCloneCloneData_Person__(global::CloneData.Person[]? source, FcGeneratedCloneState? state)
{
if (source == null) return null;
if (state != null)
{
var known = state.GetKnownRef(source);
if (known != null) return (global::CloneData.Person[])known;
}
var result = new global::CloneData.Person[source.Length];
state?.AddKnownRef(source, result);
for (int i = 0; i < source.Length; i++)
{
result[i] = source[i]?.FastDeepClone();
}
return result;
}
}
}
[/code]
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/FastCloner