示例#1
0
        /// <summary>
        /// Indexer.
        /// </summary>
        /// <remarks>
        /// The main reason this indexer is being overridden is so that we can
        /// flag as "dirty" the config source that hosts the settings.
        /// </remarks>
        public virtual object this[string key]
        {
            get
            {
                if (_maxSettingsCacheDuration != TimeSpan.MaxValue && DateTime.Now > _lastReadTimestamp + _maxSettingsCacheDuration)
                {
                    _parentConfigSource.Read();
                }
                lock (this)
                    return(_settings.ContainsKey(key) ? _settings[key] : null);
            }
            set
            {
                var valueChanged = false;
                lock (this)
                {
                    if (value == null && RemoveSettingWhenSetToNull)
                    {
                        if (_settings.ContainsKey(key))
                        {
                            _settings.Remove(key);
                            valueChanged = true;
                        }
                    }
                    else
                    {
                        if (_settings.ContainsKey(key))
                        {
                            if (_settings[key] != value)
                            {
                                _settings[key] = value;
                                valueChanged   = true;
                            }
                        }
                        else
                        {
                            _settings.Add(key, value);
                            valueChanged = true;
                        }
                    }
                }

                // We want to mark the source as "dirty".
                // We make sure the current config source inherits from our
                // concrete ConfigurationSource class, which has a MarkDirty method.
                // Other classes that just implements the IConfigurationSource interface
                // should be responsible for marking a source as dirty, since we
                // probably don't want to make MarkDirty a public method on that interface.
                if (valueChanged)
                {
                    var concreteSource = _parentConfigSource as ConfigurationSource;
                    if (concreteSource != null)
                    {
                        concreteSource.MarkDirty();
                    }
                }
            }
        }