I am a new user to GoDiagram and I am using developer license of GoDiagram professional version 22.214.171.124. I wanted to know How GoDocument’s AddCopy method works.I have a custom class which is derived from GoNode. I have overriden CopyObject() method which will be called when copying. However I see that when AddCopy() is called, copy of my custom object is created but without using COnstructor method of my custom class!!! I want to know how is this possible to create a copy without calling constructor. I had put breakpoints on all places in my class and found that only CopyObject() is called. How is this possible?? Is this Shallow copy OR deep Copy? Thanks, Nagaraj.
GoObject.CopyObject, which your override must call, uses Object.MemberwiseClone to make a bitwise copy of your class instance.
This has the good side-effect of copying all of your fields that are values.
But this has the bad side-effect of also copying all of your fields that are references to other objects, thereby resulting in the copied object sharing references to those other objects.
So your implementation of CopyObject will determine whether it is a shallow or a deep copy.
Note that GoGroup.CopyObject calls GoGroup.CopyChildren to copy each of the child objects and add them to the group. So your GoGroup/GoNode-derived class doesn't need to do this explicitly. In this sense copying a GoGroup is a deep copy.
However, if you have added fields that refer to your own data structures, your override of CopyObject can decide whether to share or copy or not share or whatever. So with respect to your own data structures, if you end up sharing references in the copy, it is a shallow copy.
Another advantage of using MemberwiseClone instead of calling the constructor is that it avoids issues with needing ReflectionPermission or that the zero-argument constructor be public. Also, this allows your constructor to do non-trivial initialization that you otherwise would not want to happen when copying.