create object from Form without Ref

Possible Duplicate:
Cloning objects in C#

My code :

private void button1_Click(object sender, EventArgs e)
{
    CopyForm(new Form1());
}

public void CopyForm(Form form)
{
    Form frm = form;
    frm.Text = "1";
    form.Text = "2";

    string c = frm.Text;// out 2
    string c2 = form.Text;// out 2
}

How to create object from Form without Ref ?

Please show me the best way.

Edit :

please sample.


You can maka copy of Form using Copy Constructor or using ICloneable's Clone method.

Given below is a simple example for Copy Constructor.You need to create you own form class and add a copyConstructor method to it.

class MyClonableForm:Form
{

public MyClonableForm(Form oldForm)//Copy Constructor
{
this.Text=oldForm.Text;
//write your clone code here
//be careful with reference types!
}

}

Note :

The use of ICloneable interface is not recommended because it does not specify the type of clone performed ie deep or shallow.

If you do want to use it,you can but dont expose the method Clone publicly.Use it for your own purpose!

链接地址: http://www.djcxy.com/p/40778.html

上一篇: 有没有办法深入复制不可序列化的对象?

下一篇: 从没有引用的表单创建对象