引用和对象克隆的奇怪问题
我在引用和对象克隆方面遇到了一个奇怪的问题,但我无法解决。 我有一个 MyClass 类,其中包含属性名称。我还有我的自定义用户控件,它具有 MyClass 类型的属性 - myclassproperty。这些控件放置在窗体上。如果我单击其中一个控件,则会出现一个新表单。我将一个参数传递给新形式 - myclassproperty
NewForm form = new NewForm(myusercontrol.myclassproperty)
this.myclassproperty = myclassproperty;
clonedproperty = ObjectCloner.Clone(myclassproperty);
clonedproperty 是一个克隆对象。对于克隆,我使用这段代码,我在 stackoverflow 上找到了它。
public static class ObjectCloner
{
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
}
在新表单中,我有一个文本框,它绑定到对象 clonedproperty
txtName.DataBindings.Add("Text",clonedproperty,"Name"); 中的归档名称;
我的表单上还有两个按钮“保存”和“取消”。如果我单击“取消”,表单就会关闭,但如果我单击“保存”,我就会这样做,
this.myclassproperty = clonedproperty
并且表单会关闭。
在我看来,此时 myusercontrol.myclassproperty 是对 clonedproperty 的引用,因此如果我再次单击 myusercontrol,则会显示新值(我之前输入的值)。
然而我一直都有旧的价值:(
I have a strange problem with reference and object cloning, which I'm not able to solve.
I have a class MyClass which consist of property Name. I also have my custon user control, which have a property of type MyClass - myclassproperty. These controls are placed on form. If I click one of control a new form apperas. I pass one argument to new form - myclassproperty
NewForm form = new NewForm(myusercontrol.myclassproperty)
this.myclassproperty = myclassproperty;
clonedproperty = ObjectCloner.Clone(myclassproperty);
clonedproperty is an cloned object. For cloning I use this code, which I found here on stackoverflow
public static class ObjectCloner
{
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
}
In new form I have a textbox which is bind to filed Name in object clonedproperty
txtName.DataBindings.Add("Text", clonedproperty, "Name");
I also have two buttons on my form ''save'' and ''cancel''. If I click cancel, form simply is closed but if I click save I do sth like that
this.myclassproperty = clonedproperty
and the form is closed.
In my opinion, at this moment myusercontrol.myclassproperty is reference to clonedproperty so if I click once again on myusercontrol the new value (which I entered previously) shows.
However I have old value all the time :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有其他事情可以继续,我会说表单可能正在使用该对象并进行更改,然后被克隆。首先尝试克隆,然后像这样打开表单。
克隆属性 = ObjectCloner.Clone(myclassproperty);
NewForm 表单 = new NewForm(myusercontrol.myclassproperty)
this.myclassproperty = myclassproperty;
如果这不起作用,您可以发布调用表单代码吗?
Without anything else to go on, I would say the form might be using the object and making the changes, and then getting cloned. Try cloning first then opening the form like this.
clonedproperty = ObjectCloner.Clone(myclassproperty);
NewForm form = new NewForm(myusercontrol.myclassproperty)
this.myclassproperty = myclassproperty;
If this doesn't work can you post the calling forms code?