.Net extension that allows you to make a shallow and deep copy of an object.
Deep copying an object, reference loops are taken into account.
Creating any copy of (shallow, deep) object does not cause any constructor.
This library allows you to easily and quickly create a copy of the object, including all the public, hidden and internal fields and properties of the object (or structure). For each type of object, during the operation, the MSIL code is generated, compiled and cached to the delegate function.
When the object is shallow copied, only the object is copied, all the reference (objects) of the field and the properties remain the same as for the original object. If deep copying of the object takes place, then all the properties and fields of the object are also copied, while the rule is observed: one object-one copy, i.e. If the original object contains a link to itself or to an object copied earlier, within the current deep copy, then the copied object will also be in a single instance.
NuGet, use
Install-Package EP.Ex
using System;
namespace EP.Ex.Tests
{
public class TestClass
{
public string Str;
private string f{ get; set; }
public TestClass(string tf)
{
Str = "Test Str";
f= tf;
}
}
public class Example{
public static void Test(){
object[] arr = new object[2] { 1, "str" };
var obj1 = arr.ShallowCopy();
//or
var obj2 = Obj<object[]>.ShallowCopy(arr);
var tc = new TestClass("MegaTest");
var dtc=tc.DeepCopy();
//or
var dtc1 = Obj.DeepCopy(tc);
///type valued
var five = (5).ShallowCopy();
}
}
} var ma =
new object[1, 1, 2, 2, 2]
{
{
{
{
{ "test", 1 },
{ 2, 2 }
},
{
{ 3, 4 },
{ "rer", 5 }
}
}
}
};
ma[0, 0, 1, 1, 1] = ma;
var ma2 = ma.DeepCopy();
var mav =
new int[1, 1, 2, 2, 2]
{
{
{
{
{ 5, 1 },
{ 2, 2 }
},
{
{ 3, 4 },
{ 7, 5 }
}
}
}
};
var mav2 = mav.DeepCopy(); var child=new ChildClass();
var obj=Obj<ParentClass>.ShallowCopy(child);
///obj.GetType() is ParentClass, not ChildClass =) var s = Obj<struct1>.New