Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpmikkers/Baksteen.Extensions.DeepCopy
C# extension method for fast object cloning. This is a speed-optimized forked version of Alexy Burtsev's deep copier.
https://github.com/jpmikkers/Baksteen.Extensions.DeepCopy
clone csharp deep-clone deep-copy deepclone deepcopy dotnet dotnet6 mit-licensed reflection
Last synced: 5 days ago
JSON representation
C# extension method for fast object cloning. This is a speed-optimized forked version of Alexy Burtsev's deep copier.
- Host: GitHub
- URL: https://github.com/jpmikkers/Baksteen.Extensions.DeepCopy
- Owner: jpmikkers
- License: mit
- Fork: true (Burtsev-Alexey/net-object-deep-copy)
- Created: 2015-12-31T14:36:30.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T11:02:47.000Z (22 days ago)
- Last Synced: 2024-11-06T01:43:19.178Z (8 days ago)
- Topics: clone, csharp, deep-clone, deep-copy, deepclone, deepcopy, dotnet, dotnet6, mit-licensed, reflection
- Language: C#
- Homepage:
- Size: 81.1 KB
- Stars: 17
- Watchers: 3
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# C# extension method for fast object cloning.
[![Nuget](https://img.shields.io/nuget/v/Baksteen.Extensions.DeepCopy)](https://www.nuget.org/packages/Baksteen.Extensions.DeepCopy/)
This is a speed-optimized fork of Alexey Burtsev's deep copier. Depending on your usecase, this will be 2x - 3x faster than the original. It also fixes some bugs which are present in the original code. Compared to the classic binary serialization/deserialization deep clone technique, this version is about seven times faster (the more arrays your objects contain, the bigger the speedup factor).
The speedup is achieved via the following techniques:
- object reflection results are cached
- don't deep copy primitives or immutable structs & classes (e.g. enum and string)
- to improve locality of reference, process the 'fast' dimensions or multidimensional arrays in the __inner__ loops
- use a compiled lamba expression to call MemberwiseClone## How to use:
Use NuGet package manager to add the package [Baksteen.Extensions.DeepCopy](https://www.nuget.org/packages/Baksteen.Extensions.DeepCopy) to your project. Then:
using Baksteen.Extensions.DeepCopy;
...
var myobject = new SomeClass();
...
var myclone = myobject.DeepCopy()!; // creates a new deep copy of the original objectNote: the exclamation mark (null-forgiving operator) is only required if you enabled nullable reference types in your project
## Contributors:
- Alexey Burtsev (original deep copy code)
- Wouter Groeneveld (unit tests & XElement copy)
- Gitkarst (treat enum as immutable)
- Jean-Paul Mikkers (speed optimization)