VOOZH about

URL: https://www.nuget.org/packages/DeepCopy.Expression/

⇱ NuGet Gallery | DeepCopy.Expression 1.5.0




DeepCopy.Expression 1.5.0

dotnet add package DeepCopy.Expression --version 1.5.0
 
 
NuGet\Install-Package DeepCopy.Expression -Version 1.5.0
 
 
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.5.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DeepCopy.Expression" Version="1.5.0" />
 
Directory.Packages.props
<PackageReference Include="DeepCopy.Expression" />
 
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 DeepCopy.Expression --version 1.5.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DeepCopy.Expression, 1.5.0"
 
 
#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 DeepCopy.Expression@1.5.0
 
 
#: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=DeepCopy.Expression&version=1.5.0
 
Install as a Cake Addin
#tool nuget:?package=DeepCopy.Expression&version=1.5.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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);
 }
}

Custom clone

From Ver1.5.0, it is now possible to pre-register customized clone processing for each type using the ObjectCloner.RegisterCustomClone method.

The following example ensures that the cloned object always have unique ID:

ObjectCloner.RegisterCustomClone(
 typeof(MyCustomizableObject),
 (Expression source, Expression destination, Expression cache) =>
 {
 var fields = CustomCloneHelper.BuildCloneFieldsExpression(
 source.Type, source, destination, cache, "_id");

 return Expression.Block(
 fields,
 CustomCloneHelper.BuildFieldAssignment(
 destination, "_id", Guid.NewGuid())
 );
 });

var orignal = new MyCustomizableObject("Foo");
var cloned = ObjectCloner.Clone(instance);

Debug.Assert(clined.Id != orignal.Id);


public sealed class MyCustomizableObject
{
 private Guid _id;

 public MyCustomizableObject(string name)
 {
 _id = Guid.NewGuid();
 Name = name;
 }

 public Guid Id => _id;
 public string Name { get; }
}

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.
  • (Supported in ver1.3.0)

License

This library is under the MIT License.

Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp2.0 netcoreapp2.0 was computed.  netcoreapp2.1 netcoreapp2.1 was computed.  netcoreapp2.2 netcoreapp2.2 was computed.  netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 netstandard2.0 is compatible.  netstandard2.1 netstandard2.1 was computed. 
.NET Framework net461 net461 was computed.  net462 net462 was computed.  net463 net463 was computed.  net47 net47 was computed.  net471 net471 was computed.  net472 net472 was computed.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen40 tizen40 was computed.  tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DeepCopy.Expression:

Package Downloads
Saritasa.NetForge

The NetForge is a library that provides a user-friendly and intuitive user interface for performing CRUD operations on your database entities within your .NET applications.

FastCloner.Benchmark

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on DeepCopy.Expression:

Repository Stars
lofcz/FastCloner
The fastest deep cloning library for .NET – zero-config, works out of the box.
Version Downloads Last Updated
1.5.0 18,378 3/4/2025
1.5.0-alpha.1 164 2/13/2025
1.4.2 1,449 11/10/2024
1.4.1 11,659 2/24/2024
1.4.0 6,367 8/19/2023
1.3.0 40,543 4/4/2021
1.2.1 1,153 5/14/2019
1.2.0 771 5/2/2019
1.1.1 810 4/29/2019
1.1.0 792 3/21/2019
1.0.2 822 3/3/2019
1.0.1 856 2/10/2019
1.0.0 1,185 2/8/2019