Implementing Clone() using serialization in a specialised TreeNode.

S

scoobydoo

Hello,

I am trying to implement ICloneable's Clone() function, using
Serialization. However, my code causes an exception.

I have a class derived from TreeNode called "Node1".

In Node1, I have implemented Clone() using serialization.

When I call the Clone() function, the following exception is thrown at
the "Deseialize" call. ------>

"An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: The constructor to deserialize an object of
type WindowsApplication1.Node1 was not found."


Can somebody explain where I am going wrong please? To run the
example that is posted below,
select a node in the treeview, and press "button 1".

TIA,
David.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
[Serializable]
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private Node1 mClone;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

treeView1.Nodes.Add(new Node1("node1.1",1));
treeView1.Nodes[0].Nodes.Add(new Node1("node1.2",2));
treeView1.Nodes[0].Nodes.Add(new Node1("node1.3",3));
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(32,
24);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(121,
192);
this.treeView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(192,
72);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,
13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs
e)
{
mClone =
(Node1)((Node1)treeView1.SelectedNode).Clone();

}
}


[Serializable]
public class Node1 : TreeNode,ICloneable
{
public int mI;

public Node1():base()
{
}

public Node1(String s, int myInt): base(s)
{
Text = s;
mI = myInt;
}

public override object Clone()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object into the stream.
bf.Serialize(ms, this);
//Position streem pointer back to first byte.
ms.Seek(0, SeekOrigin.Begin);
// Deserialize into another object.
Node1 cloneObject = (Node1)bf.Deserialize(ms);
// Release memory.
ms.Close();

return cloneObject;
}
};

}
 
F

Frans Bouma [C# MVP]

scoobydoo said:
Hello,

I am trying to implement ICloneable's Clone() function, using
Serialization. However, my code causes an exception.

I have a class derived from TreeNode called "Node1".

In Node1, I have implemented Clone() using serialization.

When I call the Clone() function, the following exception is thrown at
the "Deseialize" call. ------>

"An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: The constructor to deserialize an object of
type WindowsApplication1.Node1 was not found."


Can somebody explain where I am going wrong please? To run the
example that is posted below,
select a node in the treeview, and press "button 1".

You derive from TreeNode. This class implements ISerializable. The CTor of
TreeNode which is used for deserialization should be called from a
deserialization CTor in your class. The treenode CTor however is declared
internal: internal TreeNode(SerializationInfo object:1 si, StreamingContext
object:2 context);

So you can't call it from a ctor you're implementing.

What I'd do however is simply derive from TreeNode, and override Clone().
Clone() is implemented in TreeNode, so I'd use that one and use that data in
that copied node to fill your own copy.

FB
 
A

Andy Gaskell

As a side note - http://blogs.msdn.com/brada/archive/2003/04/09/49935.aspx
and http://blogs.msdn.com/brada/archive/2004/05/03/125427.aspx

scoobydoo said:
Hello,

I am trying to implement ICloneable's Clone() function, using
Serialization. However, my code causes an exception.

I have a class derived from TreeNode called "Node1".

In Node1, I have implemented Clone() using serialization.

When I call the Clone() function, the following exception is thrown at
the "Deseialize" call. ------>

"An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: The constructor to deserialize an object of
type WindowsApplication1.Node1 was not found."


Can somebody explain where I am going wrong please? To run the
example that is posted below,
select a node in the treeview, and press "button 1".

TIA,
David.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
[Serializable]
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Button button1;
private Node1 mClone;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

treeView1.Nodes.Add(new Node1("node1.1",1));
treeView1.Nodes[0].Nodes.Add(new Node1("node1.2",2));
treeView1.Nodes[0].Nodes.Add(new Node1("node1.3",3));
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(32,
24);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(121,
192);
this.treeView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(192,
72);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,
13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs
e)
{
mClone =
(Node1)((Node1)treeView1.SelectedNode).Clone();

}
}


[Serializable]
public class Node1 : TreeNode,ICloneable
{
public int mI;

public Node1():base()
{
}

public Node1(String s, int myInt): base(s)
{
Text = s;
mI = myInt;
}

public override object Clone()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
// Serialize the object into the stream.
bf.Serialize(ms, this);
//Position streem pointer back to first byte.
ms.Seek(0, SeekOrigin.Begin);
// Deserialize into another object.
Node1 cloneObject = (Node1)bf.Deserialize(ms);
// Release memory.
ms.Close();

return cloneObject;
}
};

}
 
S

scoobydoo

Hi,
Thank you for your answers! Cleared things up.

However, I now have a problem with cloning....

class A:TreeNode
{
int mA;

.....

public A Clone()
{
A clonedA = new A();

clonedA.mA = this.mA;

return clonedA;
}

}

class B:A
{
int mB;

.....

public B Clone()
{
B clonedB = new B();

b.mB = this.mB;

return clonedB;
}
}



My question is with class "B". If I call "base.Clone()", and assign it
to "clonedB", then "clonedB" has only been allocated enough memory for
an Instance of class "A" (because "A.Clone" returns an object of type
"A"). Consequently, there is no space allocated to store members of B,
i.e. "mB".

Simply put, how do I populate "clonedB" with the information in its
base class, "A" ?

I'm missing something!

TIA,
David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top