Small but effective deep copy extension using object reflection. It has various optimizations so it remains surprisingly fast, about 7 times faster than the usual binary serialization/deserialization trick.
$ dotnet add package Baksteen.Extensions.DeepCopyThis 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:
Use NuGet package manager to add the package 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 object
Note: the exclamation mark (null-forgiving operator) is only required if you enabled nullable reference types in your project