/// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">The key of the value to get or set.</param> /// <returns>The value associated with the specified key. If the specified key is not found, a get operation throws a System.Collections.Generic.KeyNotFoundException, and a set operation creates a new element with the specified key.</returns> public new TValue this[TKey key] { get { return(base[key]); } set { if (key != null) { ChangesList changes = this.Enlist(); if (!this.ContainsKey(key)) { changes.Add( delegate { base[key] = value; }, delegate { base.Remove(key); }); } else { TValue oldValue = base[key]; changes.Add( delegate { base[key] = value; }, delegate { base[key] = oldValue; }); } } base[key] = value; } }
/// <summary>Adds the specified key and value to the dictionary.</summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add. The value can be null for reference types.</param> public new void Add(TKey key, TValue value) { if (key != null && !this.ContainsKey(key)) { ChangesList changes = this.Enlist(); changes.Add( delegate { base.Add(key, value); }, delegate { base.Remove(key); }); } base.Add(key, value); }
/// <summary> /// Removes all keys and values from the System.Collections.Generic.Dictionary<TKey,TValue>. /// </summary> public new void Clear() { ChangesList changes = this.Enlist(); Dictionary <TKey, TValue> copy = new Dictionary <TKey, TValue>(this); changes.Add( delegate { base.Clear(); }, delegate { foreach (TKey key in copy.Keys) { base.Add(key, copy[key]); } }); base.Clear(); }
/// <summary> /// Removes the value with the specified key from the /// System.Collections.Generic.Dictionary<TKey,TValue>. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns> /// true if the element is successfully found and removed; /// otherwise, false. This method returns false if key is not found in /// the System.Collections.Generic.Dictionary<TKey,TValue>. /// </returns> public new bool Remove(TKey key) { if (base.TryGetValue(key, out TValue value)) { ChangesList changes = Enlist(); changes.Add( delegate { base.Remove(key); }, delegate { base.Add(key, value); }); return(base.Remove(key)); } else { return(false); } }
public void UpdateConfigFile(CLogConfigurationFile newConfigFile) { // Ideally, we'd use CLogConfigurationFile.AreWeDirty, but because of the tricks // we do around serialization, we can't without a major refactor if (newConfigFile == null || ConfigFile == null) { AreDirty = true; ConfigFile = newConfigFile; return; } // Serialize old config bool old = ConfigFile.SerializeChainedConfigurations; ConfigFile.SerializeChainedConfigurations = true; string serializedOldConfig = JsonConvert.SerializeObject(ConfigFile); ConfigFile.SerializeChainedConfigurations = old; // Serialize new config old = newConfigFile.SerializeChainedConfigurations; newConfigFile.SerializeChainedConfigurations = true; string serializedNewConfig = JsonConvert.SerializeObject(newConfigFile); newConfigFile.SerializeChainedConfigurations = old; if (serializedOldConfig != serializedNewConfig) { AreDirty = true; ChangesList.Add("Configuration file or dependencies changed"); } ConfigFile = newConfigFile; }