internal static bool SwitchKeys(this ModelItemDictionary dictionary, ModelItem oldKey, ModelItem newKey) { if (null == dictionary) { throw FxTrace.Exception.AsError(new ArgumentNullException("dictionary")); } if (null == oldKey) { throw FxTrace.Exception.AsError(new ArgumentNullException("oldKey")); } if (null == newKey) { throw FxTrace.Exception.AsError(new ArgumentNullException("newKey")); } if (!dictionary.ContainsKey(oldKey)) { throw FxTrace.Exception.AsError(new KeyNotFoundException(null == oldKey.GetCurrentValue() ? "oldKey" : oldKey.GetCurrentValue().ToString())); } bool result = false; if (!dictionary.ContainsKey(newKey)) { ModelItem value = dictionary[oldKey]; dictionary.Remove(oldKey); dictionary[newKey] = value; result = true; } return(result); }
internal static bool SwitchKeys(this ModelItemDictionary dictionary, object oldKey, object newKey, out ModelItem newKeyItem) { if (null == dictionary) { throw FxTrace.Exception.AsError(new ArgumentNullException("dictionary")); } if (null == oldKey) { throw FxTrace.Exception.AsError(new ArgumentNullException("oldKey")); } if (null == newKey) { throw FxTrace.Exception.AsError(new ArgumentNullException("newKey")); } if (!dictionary.ContainsKey(oldKey)) { throw FxTrace.Exception.AsError(new KeyNotFoundException(oldKey.ToString())); } bool result = false; newKeyItem = null; if (typeof(ModelItem).IsAssignableFrom(oldKey.GetType()) && typeof(ModelItem).IsAssignableFrom(newKey.GetType())) { result = SwitchKeys(dictionary, (ModelItem)oldKey, (ModelItem)newKey); newKeyItem = (ModelItem)newKey; } else { if (typeof(ModelItem).IsAssignableFrom(oldKey.GetType())) { oldKey = ((ModelItem)oldKey).GetCurrentValue(); if (null == oldKey) { throw FxTrace.Exception.AsError(new InvalidOperationException("((ModelItem)oldKey).GetCurrentValue()")); } } if (typeof(ModelItem).IsAssignableFrom(newKey.GetType())) { newKey = ((ModelItem)newKey).GetCurrentValue(); if (null == newKey) { throw FxTrace.Exception.AsError(new InvalidOperationException("((ModelItem)newKey).GetCurrentValue()")); } } } if (!dictionary.ContainsKey(newKey)) { ModelItem value = dictionary[oldKey]; dictionary.Remove(oldKey); dictionary[newKey] = value; newKeyItem = dictionary.Keys.First <ModelItem>(p => object.Equals(p.GetCurrentValue(), newKey)); result = true; } return(result); }