Пример #1
0
        /// <summary>
        /// Adds an object to be tracked by this <see cref="ObjectStatePersister"/> and loads the previous settings
        /// for the object if possible.
        /// </summary>
        /// <param name="key">Unique identifier of the <see cref="IPersistable"/> object.</param>
        /// <param name="obj"><see cref="IPersistable"/> object to load and save the settings for.</param>
        /// <exception cref="ArgumentException">An object with the given <paramref name="key"/> already exists
        /// in this collection.</exception>
        public void Add(string key, IPersistable obj)
        {
            // Add to the gui items to track
            _objs.Add(key, obj);

            // Check if the settings need to be restored
            IValueReader valueReader;

            if (_loadedNodeItems.TryGetValue(key, out valueReader))
            {
                try
                {
                    obj.ReadState(valueReader);
                }
                catch (Exception ex)
                {
                    // If we fail to read the state, just absorb the exception while printing an error
                    const string errmsg = "Failed to load settings for object `{0}` with key `{1}`: {2}";
                    var          err    = string.Format(errmsg, obj, key, ex);
                    log.Error(err);
                    Debug.Fail(err);
                }

                _loadedNodeItems.Remove(key);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds an object to be tracked by this <see cref="ObjectStatePersister"/> and loads the previous settings
        /// for the object if possible.
        /// </summary>
        /// <param name="key">Unique identifier of the <see cref="IPersistable"/> object.</param>
        /// <param name="obj"><see cref="IPersistable"/> object to load and save the settings for.</param>
        /// <exception cref="ArgumentException">An object with the given <paramref name="key"/> already exists
        /// in this collection.</exception>
        public void Add(string key, IPersistable obj)
        {
            // Add to the gui items to track
            _objs.Add(key, obj);

            // Check if the settings need to be restored
            IValueReader valueReader;
            if (_loadedNodeItems.TryGetValue(key, out valueReader))
            {
                try
                {
                    obj.ReadState(valueReader);
                }
                catch (Exception ex)
                {
                    // If we fail to read the state, just absorb the exception while printing an error
                    const string errmsg = "Failed to load settings for object `{0}` with key `{1}`: {2}";
                    var err = string.Format(errmsg, obj, key, ex);
                    log.Error(err);
                    Debug.Fail(err);
                }

                _loadedNodeItems.Remove(key);
            }
        }