Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Collections / DictionaryBase.cs / 1 / DictionaryBase.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //------------------------------------------------------------------------------ //----------------------------------------------------------------------------- namespace System.Collections { using System; using System.Security.Permissions; // Useful base class for typed read/write collections where items derive from object [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public abstract class DictionaryBase : IDictionary { Hashtable hashtable; protected Hashtable InnerHashtable { get { if (hashtable == null) hashtable = new Hashtable(); return hashtable; } } protected IDictionary Dictionary { get { return (IDictionary) this; } } public int Count { // to avoid newing inner list if no items are ever added get { return hashtable == null ? 0 : hashtable.Count; } } bool IDictionary.IsReadOnly { get { return InnerHashtable.IsReadOnly; } } bool IDictionary.IsFixedSize { get { return InnerHashtable.IsFixedSize; } } bool ICollection.IsSynchronized { get { return InnerHashtable.IsSynchronized; } } ICollection IDictionary.Keys { get { return InnerHashtable.Keys; } } Object ICollection.SyncRoot { get { return InnerHashtable.SyncRoot; } } ICollection IDictionary.Values { get { return InnerHashtable.Values; } } public void CopyTo(Array array, int index) { InnerHashtable.CopyTo(array, index); } object IDictionary.this[object key] { get { object currentValue = InnerHashtable[key]; OnGet(key, currentValue); return currentValue; } set { OnValidate(key, value); bool keyExists = true; Object temp = InnerHashtable[key]; if( temp == null) { keyExists = InnerHashtable.Contains(key); } OnSet(key, temp, value); InnerHashtable[key] = value; try { OnSetComplete(key, temp, value); } catch { if( keyExists) { InnerHashtable[key] = temp; } else { InnerHashtable.Remove(key); } throw; } } } bool IDictionary.Contains(object key) { return InnerHashtable.Contains(key); } void IDictionary.Add(object key, object value) { OnValidate(key, value); OnInsert(key, value); InnerHashtable.Add(key, value); try { OnInsertComplete(key, value); } catch { InnerHashtable.Remove(key); throw; } } public void Clear() { OnClear(); InnerHashtable.Clear(); OnClearComplete(); } void IDictionary.Remove(object key) { if(InnerHashtable.Contains(key)) { Object temp = InnerHashtable[key]; OnValidate(key, temp); OnRemove(key, temp); InnerHashtable.Remove(key); try { OnRemoveComplete(key, temp); } catch { InnerHashtable.Add(key, temp); throw; } } } public IDictionaryEnumerator GetEnumerator() { return InnerHashtable.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return InnerHashtable.GetEnumerator(); } protected virtual object OnGet(object key, object currentValue) { return currentValue; } protected virtual void OnSet(object key, object oldValue, object newValue) { } protected virtual void OnInsert(object key, object value) { } protected virtual void OnClear() { } protected virtual void OnRemove(object key, object value) { } protected virtual void OnValidate(object key, object value) { } protected virtual void OnSetComplete(object key, object oldValue, object newValue) { } protected virtual void OnInsertComplete(object key, object value) { } protected virtual void OnClearComplete() { } protected virtual void OnRemoveComplete(object key, object value) { } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //------------------------------------------------------------------------------ //----------------------------------------------------------------------------- namespace System.Collections { using System; using System.Security.Permissions; // Useful base class for typed read/write collections where items derive from object [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public abstract class DictionaryBase : IDictionary { Hashtable hashtable; protected Hashtable InnerHashtable { get { if (hashtable == null) hashtable = new Hashtable(); return hashtable; } } protected IDictionary Dictionary { get { return (IDictionary) this; } } public int Count { // to avoid newing inner list if no items are ever added get { return hashtable == null ? 0 : hashtable.Count; } } bool IDictionary.IsReadOnly { get { return InnerHashtable.IsReadOnly; } } bool IDictionary.IsFixedSize { get { return InnerHashtable.IsFixedSize; } } bool ICollection.IsSynchronized { get { return InnerHashtable.IsSynchronized; } } ICollection IDictionary.Keys { get { return InnerHashtable.Keys; } } Object ICollection.SyncRoot { get { return InnerHashtable.SyncRoot; } } ICollection IDictionary.Values { get { return InnerHashtable.Values; } } public void CopyTo(Array array, int index) { InnerHashtable.CopyTo(array, index); } object IDictionary.this[object key] { get { object currentValue = InnerHashtable[key]; OnGet(key, currentValue); return currentValue; } set { OnValidate(key, value); bool keyExists = true; Object temp = InnerHashtable[key]; if( temp == null) { keyExists = InnerHashtable.Contains(key); } OnSet(key, temp, value); InnerHashtable[key] = value; try { OnSetComplete(key, temp, value); } catch { if( keyExists) { InnerHashtable[key] = temp; } else { InnerHashtable.Remove(key); } throw; } } } bool IDictionary.Contains(object key) { return InnerHashtable.Contains(key); } void IDictionary.Add(object key, object value) { OnValidate(key, value); OnInsert(key, value); InnerHashtable.Add(key, value); try { OnInsertComplete(key, value); } catch { InnerHashtable.Remove(key); throw; } } public void Clear() { OnClear(); InnerHashtable.Clear(); OnClearComplete(); } void IDictionary.Remove(object key) { if(InnerHashtable.Contains(key)) { Object temp = InnerHashtable[key]; OnValidate(key, temp); OnRemove(key, temp); InnerHashtable.Remove(key); try { OnRemoveComplete(key, temp); } catch { InnerHashtable.Add(key, temp); throw; } } } public IDictionaryEnumerator GetEnumerator() { return InnerHashtable.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return InnerHashtable.GetEnumerator(); } protected virtual object OnGet(object key, object currentValue) { return currentValue; } protected virtual void OnSet(object key, object oldValue, object newValue) { } protected virtual void OnInsert(object key, object value) { } protected virtual void OnClear() { } protected virtual void OnRemove(object key, object value) { } protected virtual void OnValidate(object key, object value) { } protected virtual void OnSetComplete(object key, object oldValue, object newValue) { } protected virtual void OnInsertComplete(object key, object value) { } protected virtual void OnClearComplete() { } protected virtual void OnRemoveComplete(object key, object value) { } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SignatureDescription.cs
- BaseHashHelper.cs
- TranslateTransform3D.cs
- RoutedEventConverter.cs
- OracleBoolean.cs
- ActivationArguments.cs
- QilPatternFactory.cs
- SystemMulticastIPAddressInformation.cs
- MemoryStream.cs
- TrackingServices.cs
- Validator.cs
- ViewCellSlot.cs
- TextParaClient.cs
- ObjectFactoryCodeDomTreeGenerator.cs
- XmlDocument.cs
- ObjectTypeMapping.cs
- ContentControl.cs
- SqlStream.cs
- IPHostEntry.cs
- SmtpTransport.cs
- Queue.cs
- WindowsListViewScroll.cs
- TimeSpanSecondsOrInfiniteConverter.cs
- WindowsEditBoxRange.cs
- WindowsToolbar.cs
- XmlUtil.cs
- XmlnsDefinitionAttribute.cs
- XMLUtil.cs
- hebrewshape.cs
- HttpVersion.cs
- AccessDataSourceView.cs
- WebHttpBehavior.cs
- AssemblyCollection.cs
- UnmanagedHandle.cs
- HealthMonitoringSection.cs
- SoapInteropTypes.cs
- QEncodedStream.cs
- AppLevelCompilationSectionCache.cs
- ContextActivityUtils.cs
- ResourceContainer.cs
- FilteredDataSetHelper.cs
- SpecularMaterial.cs
- WizardStepBase.cs
- RuntimeResourceSet.cs
- SimpleBitVector32.cs
- documentsequencetextcontainer.cs
- CommonDialog.cs
- TreeSet.cs
- ScopedKnownTypes.cs
- peersecuritysettings.cs
- SafeMILHandleMemoryPressure.cs
- ButtonBaseAutomationPeer.cs
- RegexStringValidatorAttribute.cs
- EnumMember.cs
- Frame.cs
- SortKey.cs
- StackBuilderSink.cs
- SizeValueSerializer.cs
- MouseDevice.cs
- KnownIds.cs
- ViewUtilities.cs
- ReadOnlyPropertyMetadata.cs
- ScriptingJsonSerializationSection.cs
- SafeNativeMethods.cs
- PropertyTabChangedEvent.cs
- CompiledRegexRunnerFactory.cs
- NavigationFailedEventArgs.cs
- WorkerRequest.cs
- PropertyGridEditorPart.cs
- ResourceIDHelper.cs
- WhitespaceRuleLookup.cs
- StringUtil.cs
- LineGeometry.cs
- Debug.cs
- OleStrCAMarshaler.cs
- FileEnumerator.cs
- SocketElement.cs
- XhtmlStyleClass.cs
- HtmlSelect.cs
- ProfilePropertyNameValidator.cs
- InvalidContentTypeException.cs
- LinkClickEvent.cs
- Bidi.cs
- WebSysDescriptionAttribute.cs
- SecurityException.cs
- DebugHandleTracker.cs
- ColorPalette.cs
- TextTreeExtractElementUndoUnit.cs
- TreeNodeSelectionProcessor.cs
- Walker.cs
- UnSafeCharBuffer.cs
- ComponentDispatcher.cs
- Viewport3DVisual.cs
- DataGridColumnDropSeparator.cs
- columnmapkeybuilder.cs
- WebConfigurationHostFileChange.cs
- WindowsSecurityTokenAuthenticator.cs
- baseaxisquery.cs
- OletxEnlistment.cs
- EndpointConfigContainer.cs