IDeepCloneable.Generator
0.0.10
See the version list below for details.
dotnet add package IDeepCloneable.Generator --version 0.0.10
NuGet\Install-Package IDeepCloneable.Generator -Version 0.0.10
<PackageReference Include="IDeepCloneable.Generator" Version="0.0.10"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="IDeepCloneable.Generator" Version="0.0.10" />
<PackageReference Include="IDeepCloneable.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add IDeepCloneable.Generator --version 0.0.10
#r "nuget: IDeepCloneable.Generator, 0.0.10"
#:package IDeepCloneable.Generator@0.0.10
#addin nuget:?package=IDeepCloneable.Generator&version=0.0.10
#tool nuget:?package=IDeepCloneable.Generator&version=0.0.10
IDeepCloneable
Automatic implementation of the IDeepCloneable<T> interface via source generators. For library authors.
Overview
Provides automatic generation of the DeepClone() method and IDeepCloneable<T> implementation for partial types marked with [DeepCloneable].
Benefits
While there are many similar libraries available, this library's key feature is that it generates the DeepClone() method as an implementation of the IDeepCloneable<T> interface.
By doing this:
- Library authors can use
DeepClone()without reflection (NativeAOT friendly) - Users are relieved of the burden of manual implementation
// 3rd-party library side
public void RegisterCloneMethod<T>()
{
Func<T, T> cloneFunc = null;
if(typeof(IDeepCloneable<T>).IsAssignableFrom(typeof(T))) {
cloneFunc = obj => obj.DeepClone();
}
else {
// fallback implementation
}
}
// user side
[DeepCloneable]
public partial class MyModel { /* ... */ }
How to use
Basic Usage
Install the NuGet package IDeepCloneable to your project.
dotnet add package IDeepCloneable
Then mark a partial type with the [DeepCloneable] attribute.
using IDeepCloneable;
[DeepCloneable]
public partial class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
That's it! The DeepClone() method will be automatically generated and the generated partial type will implement IDeepCloneable<Person>.
// generated code (sample)
partial class Person : IDeepCloneable<Person>
{
public Person DeepClone()
{
return new Person
{
Name = this.Name,
Age = this.Age,
};
}
}
And you can use it like this:
var person1 = new Person { Name = "Alice", Age = 30 };
var person2 = person1.DeepClone();
person2.ShouldNotBeSameAs(person1);
Customize
As you can see from the generated code, you can simply implement the IDeepCloneable<T>.DeepClone() method yourself.
using IDeepCloneable;
public class Person : IDeepCloneable<Person>
{
public string Name { get; set; }
public int Age { get; set; }
public Person DeepClone()
{
// your custom implementation
}
}
Library Structure
This consists of two libraries.
IDeepCloneable
This is the library that defines the IDeepCloneable<T> interface and the [DeepCloneable] marker attribute.
namespace IDeepCloneable;
public sealed class DeepCloneableAttribute : Attribute;
public interface IDeepCloneable<T>
{
T DeepClone();
}
Additionally, it will automatically reference the IDeepCloneable.Generator.
IDeepCloneable.Generator
This is the source generator library that automatically generates the IDeepCloneable<T>.DeepClone() method.
There is no need to directly reference this library.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 0.0.27 | 209 | 1/12/2026 | |
| 0.0.26 | 149 | 1/12/2026 | |
| 0.0.25 | 151 | 1/12/2026 | |
| 0.0.24 | 154 | 1/12/2026 | |
| 0.0.23 | 155 | 1/12/2026 | |
| 0.0.22 | 156 | 1/12/2026 | |
| 0.0.21 | 156 | 1/11/2026 | |
| 0.0.20 | 156 | 1/9/2026 | |
| 0.0.10 | 158 | 1/6/2026 | |
| 0.0.5 | 156 | 1/4/2026 | |
| 0.0.4 | 155 | 1/2/2026 | |
| 0.0.3 | 156 | 1/1/2026 | |
| 0.0.2 | 156 | 12/31/2025 | |
| 0.0.1 | 155 | 12/31/2025 |