Copy an instance
-
Is it possible to make an exact copy of an instance of an object (not its reference)? This is what I'd like to achieve: 1 make a copy of an instance at a certain moment to save the state it's in 2 modify the original for a bit 3 replace the original object with the saved one later on This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
To do this, you can binary serialize the object. Serialize the object to a memory stream and then deserialize to a new instance. For classes that don't have a CopyTo method, this is pretty helpful. However, the class that you're serializing has to have the
Serializable
attribute.
Logifusion[^] If not entertaining, write your Congressman.
-
Is it possible to make an exact copy of an instance of an object (not its reference)? This is what I'd like to achieve: 1 make a copy of an instance at a certain moment to save the state it's in 2 modify the original for a bit 3 replace the original object with the saved one later on This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
In short you want something to that gives you an 'undo' feature. I don't like recommending 'design patterns' without looking at several alternatives, but in this case the Memento pattern fits well. Googling will give you a solid explanation. Wikipedia has a short code sample: Memento pattern[^]
Recitation(not full prayer)in AlMasjid AlHaram Surah AlHaaqa(The Reality) Surah Qaf Eid Alfitr Turning Muslim in Texas?
-
Is it possible to make an exact copy of an instance of an object (not its reference)? This is what I'd like to achieve: 1 make a copy of an instance at a certain moment to save the state it's in 2 modify the original for a bit 3 replace the original object with the saved one later on This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
You could do the following: Make the following changes to your class:
public class MyClass : ICloneable
{
public MyClass(){}
public MyClass(object newInstance)
{
if ( !(newInstance is MyClass) ) throw new Exception...
MyClass instance = (MyClass)newInstance;
// set all properties to the values in the passed instance
}
public object Clone
{
return (object)this;
{
}Then in your code you would do as follows:
... MyClass newObject = new MyClass(oldObject.Clone());
Unfortunately, there is no ICloneable and object is all you can pass.
-
Is it possible to make an exact copy of an instance of an object (not its reference)? This is what I'd like to achieve: 1 make a copy of an instance at a certain moment to save the state it's in 2 modify the original for a bit 3 replace the original object with the saved one later on This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
Wesner Moise has a good blog posting[^] regarding how to clone objects that don't implement ICloneable. He basically uses reflection to invoke the protected MemberwiseClone method. If you have the source code to this object, I'd just make it implement ICloneable.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
In short you want something to that gives you an 'undo' feature. I don't like recommending 'design patterns' without looking at several alternatives, but in this case the Memento pattern fits well. Googling will give you a solid explanation. Wikipedia has a short code sample: Memento pattern[^]
Recitation(not full prayer)in AlMasjid AlHaram Surah AlHaaqa(The Reality) Surah Qaf Eid Alfitr Turning Muslim in Texas?
-
To do this, you can binary serialize the object. Serialize the object to a memory stream and then deserialize to a new instance. For classes that don't have a CopyTo method, this is pretty helpful. However, the class that you're serializing has to have the
Serializable
attribute.
Logifusion[^] If not entertaining, write your Congressman.
Actually it is
Serializable
since it can be stored to disk too. I just had a go with this but I haven't serialized to memory before and for some reason I'm getting aSystem.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
The code looks like this.Collection myOutputCollection = new Collection();
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
OutputData myOutput;bFormatter.Serialize(mStream, myOutput);
deserializedOutput = (OutputData)bFormatter.Deserialize(mStream);
myOutputCollection.add(deserializedOutput);
mStream.Close();
I'm serializing and deserializing the object, then adding it to a collection, since I'm still using it from the collection once it's added (it's not like I don't need it anymore).
-
Wesner Moise has a good blog posting[^] regarding how to clone objects that don't implement ICloneable. He basically uses reflection to invoke the protected MemberwiseClone method. If you have the source code to this object, I'd just make it implement ICloneable.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Actually it is
Serializable
since it can be stored to disk too. I just had a go with this but I haven't serialized to memory before and for some reason I'm getting aSystem.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
The code looks like this.Collection myOutputCollection = new Collection();
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
OutputData myOutput;bFormatter.Serialize(mStream, myOutput);
deserializedOutput = (OutputData)bFormatter.Deserialize(mStream);
myOutputCollection.add(deserializedOutput);
mStream.Close();
I'm serializing and deserializing the object, then adding it to a collection, since I'm still using it from the collection once it's added (it's not like I don't need it anymore).
Before you deserialize, you have to rewind the memory stream back to the initial position.
Logifusion[^] If not entertaining, write your Congressman.
-
Before you deserialize, you have to rewind the memory stream back to the initial position.
Logifusion[^] If not entertaining, write your Congressman.
-
Is it possible to make an exact copy of an instance of an object (not its reference)? This is what I'd like to achieve: 1 make a copy of an instance at a certain moment to save the state it's in 2 modify the original for a bit 3 replace the original object with the saved one later on This way I'd like to be able to "recall" the state my object was in at the moment I made a copy. Is this possible without manually constructing a new object of this type and copying all of it's members?
I'm doing this using the
Clone
method of the object. I think there's no universal mechanism for copying objects, so you need to implement this method by your own. If you are working with more types, it is good to use someICloneable
interface. This is an example:public interface ICloneable { object Clone(); } public class BitmapWithArray : ICloneable { public Bitmap bitmap = null; public int[] array = null; public ushort someValue = 0; public object Clone() { BitmapWithArray obj = new BitmapWithArray(); if (this.bitmap != null) obj.bitmap = (Bitmap)this.bitmap.Clone(); if (this.array != null) obj.array = (int[])this.array.Clone(); obj.someValue = this.someValue; // this is a value type, no copies needed } }