Skip to content
elcoolepercoPublic

Latest commit

 

History

45 Commits

Folders and files

Repository files navigation

EP.Ex

.Net extension that allows you to make a shallow and deep copy of an object.

Remark

Deep copying an object, reference loops are taken into account.

Important

Remark 2

Creating any copy of (shallow, deep) object does not cause any constructor.

Description

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.

Installation

NuGet, use Install-Package EP.Ex

Example:

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();
            }
        }
}

Multi Dimentional:

            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();

Another nice addition is the ability to complete 'upcast' to the parent class:

  var child=new ChildClass();
  var obj=Obj<ParentClass>.ShallowCopy(child);
  ///obj.GetType() is ParentClass, not ChildClass =)

Fast create new object using constructor without parameters

   var s = Obj<struct1>.New