IDeepCloneable.Generator 0.0.10

Suggested Alternatives

IDeepCloneable

There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IDeepCloneable.Generator" Version="0.0.10">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IDeepCloneable.Generator" Version="0.0.10" />
                    
Directory.Packages.props
<PackageReference Include="IDeepCloneable.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IDeepCloneable.Generator --version 0.0.10
                    
#r "nuget: IDeepCloneable.Generator, 0.0.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package IDeepCloneable.Generator@0.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IDeepCloneable.Generator&version=0.0.10
                    
Install as a Cake Addin
#tool nuget:?package=IDeepCloneable.Generator&version=0.0.10
                    
Install as a Cake Tool

IDeepCloneable

NuGet Version GitHub Actions Workflow Status

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.

There are no supported framework assets in this package.

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.