DeepCopy.Expression 1.4.1

dotnet add package DeepCopy.Expression --version 1.4.1
NuGet\Install-Package DeepCopy.Expression -Version 1.4.1
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="DeepCopy.Expression" Version="1.4.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DeepCopy.Expression --version 1.4.1
#r "nuget: DeepCopy.Expression, 1.4.1"
#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.
// Install DeepCopy.Expression as a Cake Addin
#addin nuget:?package=DeepCopy.Expression&version=1.4.1

// Install DeepCopy.Expression as a Cake Tool
#tool nuget:?package=DeepCopy.Expression&version=1.4.1

DeepCopy.Expression

DeepCopy.Expression is a library that allows you to create deep copies of objects using expression trees. A deep copy is a copy that duplicates not only the object itself, but also the objects it references. Expression trees are data structures that represent code as a tree expressions.

Install

To install the library, you can use the following command in the Package Manager Console

PM > Install-Package DeepCopy.Expression

Quick start

To use the library, you can create a class that represents your object and call the ObjectCloner.Clone method to create a deep copy of it. For example:

class MyObject
{
    public MyObject(int id)
    {
        Id = id;
    }

    public int Id { get; }
    public string Name { get; set; }
    public List<int> List { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var obj = new MyObject(1)
        {
            Name = "Hoge",
            List = new List<int> { 10, 20, 30 }
        };
        
        var cloned = ObjectCloner.Clone(obj);
    }
}

The library also supports anonymous types, which are types that are inferred from the data you assign to them. For example:

class Program
{
    static void Main(string[] args)
    {
        var obj = new
        {
            Id = 1,
            Name = "Hoge",
            List = new List<int> { 10, 20, 30 }
        };
        
        var cloned = ObjectCloner.Clone(obj);
    }
}

You can customize the copy behavior of your classes by using the [Cloneable] attribute and the [CopyMember] attribute. The [Cloneable] attribute marks a class as cloneable and the [CopyMember] attribute marks a field or a property as a member to be copied. You can also specify a copy policy for each member, which determines how the member is copied. For example:

[Cloneable]
class MyObject
{
    public MyObject(int id)
    {
        Id = id;
    }

    [CopyMember]
    public int Id { get; }
    public string Name { get; set; }
    [CopyMember(CopyPolicy.ShallowCopy)]
    public List<int> List { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var obj = new MyObject(1)
        {
            Name = "Hoge",
            List = new List<int> { 10, 20, 30 }
        };
        
        var cloned = ObjectCloner.Clone(obj);
    }
}

Copy policy

The available copy policies are:

  • Default: The default policy for the type of the member. For value types, it performs an assignment, For reference tytpes, it performs a deep copy. For arrays, it performs a clone. For delegates, it performs an assignment.
  • DeepCopy: Performs a deep copy of the member regardless of its type.
  • ShallowCopy: Performs a shallow copy of the member regardless of its type. Shallow copy is a copy that duplicates only object itself, but not the objects it references.
  • Assign: Performs an assignment to the member regardless of its type.
ValueType Class /<br>Struct with reference Array(ValueType) Array(Class) Delegate
Default Assign DeepCopy Clone DeepCopy Assgin
DeepCopy Assign DeepCopy DeepCopy DeepCopy Assgin
ShallowCopy Assgin MemberwiseClone Clone Clone Assgin
Assign Assgin Assgin Assgin Assgin Assgin

Performance

This is a benchmark of TestObject's deep clone. The performance of the library is comparable to the code that is specially implemented for deep copying. The library uses caching to avoid generating expression trees every time. The first time you clone an object, it may take longer than subsequent times.

Method Mean Error StdDev Ratio Gen 0
CloneWithImprementation 41.77 us 1.4089 us 4.0425 us 1.00 30.0293
CloneWithSerialization 698.42 us 5.5453 us 4.9158 us 15.57 179.6875
CloneWithExpressionFirstTimeOnly 4,242.48 us 84.4623 us 97.2669 us 97.07 179.6875
CloneWithExpression 42.37 us 0.5604 us 0.5242 us 0.95 27.8931

Limitations

The library has some limitations:

  • It does not copy delegates.
  • It does not support direct array specification. (Supported in ver1.3.0)

License

This library is under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • 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
1.4.1 368 2/24/2024
1.4.0 1,058 8/19/2023
1.3.0 15,662 4/4/2021
1.2.1 898 5/14/2019
1.2.0 521 5/2/2019
1.1.1 527 4/29/2019
1.1.0 534 3/21/2019
1.0.2 529 3/3/2019
1.0.1 580 2/10/2019
1.0.0 761 2/8/2019