Skip to content
Oliver Ehrenmüller edited this page Apr 13, 2019 · 27 revisions

Features

  • Mark a class with AddDeepCopyConstructor-Attribute to create a deep copy constructor.
  • Mark a static method with DeepCopyExtension-Attribute to create a nullable deep copy method that supports inheritance.
  • Mark an existing copy constructor with InjectDeepCopy to insert deep copy instructions before your code. Details
  • Mark a property with IgnoreDuringDeepCopy to skip this property.
  • Mark a property with DeepCopyByReference to copy by reference.
  • Collections of types IList<>, ISet<> and IDictionary<,> are supported and described here

Start with DeepCopyExtension

To create a compileable method you can call simply return the parameter:

[DeepCopyExtension]
public static MyType DeepCopy(this MyType myType) => myType;

or throw an exception

[DeepCopyExtension]
public static MyType DeepCopy(this MyType myType) => throw new NotImplementedException();

What gets compiled

public static MyType DeepCopy(this MyType myType) {
    return myType != null ? new MyType(myType) : null;
}

and MyType will automatically receive a deep copy constructor.

Not supported

  • Fields
  • Properties of types without copy constructor
  • Properties of type System.Object
  • Collection parameterized with System.Object like IList<object>

Clone this wiki locally