Reputation: 1457
I have my own class MyTreeNode
derived from TreeNode
:
public class MyTreeNode : System.Windows.Forms.TreeNode
{
[Localizable(true)]
public bool Foo { get; set; }
}
I try to clone the node:
MyTreeNode myTreeNode = new MyTreeNode();
myTreeNode.Foo = foo;
//
//And here is the problem, all fields have been copied to the new node but Foo
//
MyTreeNode newNode = (MyTreeNode)myTreeNode.Clone();
In result, newNode
has empty Foo
field. How can I fix this ?
Upvotes: 2
Views: 1394
Reputation: 25201
If you want Foo
to be copied as well when calling Clone()
, you'll need to override the Clone()
method and add the logic to do so.
All you need to do is add this to your MyTreeNode
class:
public override object Clone()
{
var obj = (MyTreeNode)base.Clone();
obj.Foo = this.Foo;
return obj;
}
Upvotes: 4
Reputation: 312
Try something like this in your MyTreeNode class
public override object Clone()
{
object objReturn = base.Clone();
((MyTreeNode)objReturn).Foo = this.Foo;
return objReturn;
}
Upvotes: 1