开发者

Strange problem with reference and object cloning

开发者 https://www.devze.com 2023-01-14 00:02 出处:网络
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 a开发者_如何学运维lso have my custon user control, wh

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 a开发者_如何学运维lso 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 :(


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消