Пример #1
0
        /// <summary>
        /// Undo the changes that were made since the last time 'CreateSnapshot' was called.
        /// </summary>
        public void RevertToPreviousState()
        {
            _bindingEdit = false;

            // We can only undo if we've a state to revert to.
            if (EditLevel > 0)
            {
                // Get the latest state of the object that was pushed on the state-stack
                MemoryStream buffer = new MemoryStream((byte[])_stateStack.Pop());
                buffer.Position = 0;
                BinaryFormatter fmt   = new BinaryFormatter();
                Hashtable       state = (Hashtable)fmt.Deserialize(buffer);

                Type        currentType = this.GetType();
                FieldInfo[] fields;
                string      fieldName;

                do
                {
                    fields = currentType.GetFields(BindingFlags.Public |
                                                   BindingFlags.NonPublic |
                                                   BindingFlags.Instance);

                    foreach (FieldInfo field in fields)
                    {
                        if (field.DeclaringType == currentType &&
                            this.IsUndoableField(field))
                        {
                            object fieldValue = field.GetValue(this);

                            IUndoable uf = fieldValue as IUndoable;

                            if (uf != null)
                            {
                                uf.RevertToPreviousState();
                            }
                            else
                            {
                                fieldName = currentType.Name + "." + field.Name;
                                field.SetValue(this, state[fieldName]);
                            }
                        }
                    }

                    currentType = currentType.BaseType;
                } while(currentType != typeof(object));
            }
        }