DeepCloner .NET Library

repository·develop·Indexed 20 days ago

https://github.com/force-net/deepcloner

A high-performance .NET library for deep and shallow cloning of objects using runtime code generation. It provides extension methods like DeepClone(), ShallowClone(), DeepCloneTo(), and ShallowCloneTo() to copy objects via their internal structure without invoking constructors. Supports .NET 4.0+ and .NET Standard 1.3, with automatic fallback to a Safe copying variant in restricted permission environments.

Tokens
875
Snippets
5
Records
6
Agent score
20%

What's inside DeepCloner

  1. How DeepClone and ShallowClone differ

    develop

    The primary difference is how related objects are handled:

    • DeepClone: Guarantees that all changes to the cloned object (including nested properties) do not affect the original object.
    • ShallowClone: Only copies the top-level object. Nested objects are shared between the original and the clone.
    // public class A { public B B; } 
    // public class B { public int X; }
    
    var b = new B { X = 1 };
    var a = new A { B = b };
    
    // Deep Clone Example
    var deepClone = a.DeepClone();
    deepClone.B.X = 2;
    Console.WriteLine(a.B.X); // Output: 1
    
    // Shallow Clone Example
    var shallowClone = a.ShallowClone();
    shallowClone.B.X = 2;
    Console.WriteLine(a.B.X); // Output: 2
  2. Supported Frameworks and Permissions

    develop

    DeepCloner supports the following environments:

    • .NET 4.0 or higher
    • .NET Standard 1.3 (.NET Core): Note that the .NET Standard implementation only supports the Safe copying variant, which is slightly slower than the standard variant.

    Permissions:

    • The library prefers Full Trust or Reflection permission (MemberAccess).
    • If the environment lacks these, the library automatically switches to a Safe variant which is slightly slower but more secure.
    • If you are in a highly restricted environment where even the Safe variant fails, consider using CloneExtensions (which only clones public properties).
  3. Shallow clone an object

    develop

    Use the ShallowClone() extension method to create a copy of the object itself, but without cloning the objects it references. This is faster than a deep clone but does not guarantee that changes to nested objects won't affect the original.

    var clone = new { Id = 1, Name = "222" }.ShallowClone();
  4. Deep clone an object

    develop

    Use the DeepClone() extension method to create a complete copy of an object, including all objects in its graph. This ensures that changes to the cloned object do not affect the original.

    Note: DeepCloner copies objects by their internal structure without calling any constructors or methods. It is not recommended to clone objects bound to native resources or pointers.

    var clone = new { Id = 1, Name = "222" }.DeepClone();
  5. Clone into an existing object

    develop

    You can copy the fields of a source object into an existing instance using DeepCloneTo or ShallowCloneTo.

    Requirements:

    • The target object must be a class (not a struct).
    • The target class must be a real descendant of the source class (or the same type). Casting a derived object to a base type and then attempting to clone into a different derived type will throw a runtime exception.
    public class Derived : BaseClass
    {
        public Derived(BaseClass parent)
        {
            parent.DeepCloneTo(this); // 'this' now has every field from 'parent'
        }
    }