![]() |
VOOZH | about |
dotnet add package DeepCopy.Expression --version 1.5.0
NuGet\Install-Package DeepCopy.Expression -Version 1.5.0
<PackageReference Include="DeepCopy.Expression" Version="1.5.0" />
<PackageVersion Include="DeepCopy.Expression" Version="1.5.0" />Directory.Packages.props
<PackageReference Include="DeepCopy.Expression" />Project file
paket add DeepCopy.Expression --version 1.5.0
#r "nuget: DeepCopy.Expression, 1.5.0"
#:package DeepCopy.Expression@1.5.0
#addin nuget:?package=DeepCopy.Expression&version=1.5.0Install as a Cake Addin
#tool nuget:?package=DeepCopy.Expression&version=1.5.0Install as a Cake Tool
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.
To install the library, you can use the following command in the Package Manager Console
PM > Install-Package DeepCopy.Expression
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);
}
}
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; }
}
The available copy policies are:
| 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 |
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 |
The library has some limitations:
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. |
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 |
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 |