0

I have been searching for a simple solution for a cloning of an object containing other objects.

public class TPFTestCaseTreeNode: TreeNode, ICloneable
{
    public Object Obj;

    public TPFTestCaseTreeNode(string Title, Object O)
    {
        // Set attributes for the TreeNode
        Text = Title; // not sure which one we need
        Name = Title; // not sure which one we need

        // And additionally, remember the test case object
        Obj = O;
    }
} 

For Cloning, I am using :

foreach(TreeNode t in listAllTestCases)
{
    if(t.Name.Equals(testCaseIdDesc))
    {
        theNode = (TreeNode)((ICloneable)t).DeepClone();                     
    }
}

listAllTestCases contain all the tree nodes of type "TPFTestCaseTreeNode".

"t" in the loop, does contain valid value for "Obj" as per debugger mode

I have tried the normal Clone() and DeepClone() as well, none of them was able to clone the state of the Object "Obj". It always remains null in the cloned object treenode "theNode".

Can anyone provide a plausible explanation why the cloning of an object containing another object is failing here? Here are the two states Initial before cloning and after cloning. Initial State Cloned Object

Please note that I have even tried the binaryformatter(serialize/ deserialize mechanism) as well. But still, Object "Obj" is null.

satyadev
  • 1
  • 4
  • How did you implement `DeepClone`? – Yuval Itzchakov Jul 08 '15 at 05:39
  • possible duplicate of [Deep cloning objects](http://stackoverflow.com/questions/78536/deep-cloning-objects) – Mick Jul 08 '15 at 05:43
  • This is a duplicate of http://stackoverflow.com/questions/78536/deep-cloning-objects – Mick Jul 08 '15 at 05:43
  • In the loop, t is TreeNode instead of TPFTestCaseTreeNode. It will not contain definition for Obj. – HarryQuake Jul 08 '15 at 06:17
  • I found that the best way to do deep cloning is writing your own constructor and creating a new object with the other objects data – Vajura Jul 08 '15 at 06:44
  • [link](https://goo.gl/efwkJn) Initial State [link](https://goo.gl/Sfw7T8) Cloned Object ,So in the loop, TPFTestCaseTreeNode is a type of Treenode, even if I am casting it to Treenode, should it still not maintained the properties of TPFTestCaseTreenNode? @vajura any example for Object retrieval? I have used PropertyInfo but the "Obj" is not found, I had to specify it using FieldInfo fInf = typeSource.GetField("Obj"); but obviously here it has been casted to System.Object and it's quite troublesome to cast it back when I am dealing with reflection in the run time manipulation. – satyadev Jul 08 '15 at 06:45
  • So you have a obj attribute inside your custom TreeNode class and when you cast it to just TreeNode that object attribute is lost? Pretty sure thats correct behavoiur or? Also i was doing some stuff with treeNodes also and i found it very helpful to just save objects inside .Tag property of the TreeNode – Vajura Jul 08 '15 at 06:57
  • Indeed I have the Obj attribute in my TreeNode specific Class TPFTestCaseTreeNode . I will try to do add the obj to tag property and see its behaviour. But still this is so challenging do to a simple deep clone in c# that I am having to do "hacks" – satyadev Jul 08 '15 at 09:17

1 Answers1

0

So Finally, I had to use the Treenode "Tag" property itself and assign my "Obj" to it. However here as well DeepClone() or Clone() did not clone this Tag Property value.

Basically I had to loop on each property of the actual object and build the cloned one.

Such a hassle for custom types of Treenode, but it is working fine now.

Thanks all for the valuable inputs.

satyadev
  • 1
  • 4