I'm trying to implement an Undo/Redo stack in a C# application that I'm working on by restoring an Object to a previous state when undo is called. I have an 'Action' class which looks basically like this:
class Action
{
object old_state;
object new_state;
public Action(object old)
{
old_state = old;
}
public void finish(object new_obj)
{
new_state = new_obj;
}
public void reverse()
{
new_state = old_state;
}
}
When an action is started that can be redone, a new action is created. When we reach the new state, finish() is called. When the user wants to redo something, it calls reverse() and restores the Object to its original state.
Obviously this doesn't work since both objects are passed by reference and the object just ends up in the new state.
What I really want to do is to be able to say:
public Action(object old)
{
old_state = old.MemberwiseClone();
}
unfortunately, this doesn't work, and I get an error that looks like:
Cannot access protected member 'object.MemberwiseClone()' via a qualifier of type 'foo.object'
I want to create a shallow copy of the original state (copy all of the value fields by value and all of the reference fields by reference), but I can't figure out how to do this using generic objects, as opposed to implementing IClonable
in every class of which I could possibly wish to restore the state.
Can anyone offer any insight?