Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / WeakReferenceKey.cs / 1305600 / WeakReferenceKey.cs
//---------------------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This file defines a class that holds a weak reference to an object. It preserves the hashcode // of the object and is intended to be used as a key in hashtables or dictionaries. // //------------------------------------------------------------------------------------- using System; using MS.Internal; namespace MS.Internal.Utility { ////// Helper class that allows using a weak reference to an item as a key in a hash table. /// internal class WeakReferenceKey{ public WeakReferenceKey(T item) { Invariant.Assert(item != null); _item = new WeakReference(item); _hashCode = item.GetHashCode(); } public T Item { get { return (T)_item.Target; } } public override bool Equals(object o) { if (o == this) return true; WeakReferenceKey key = o as WeakReferenceKey ; if (key != null) { T item = this.Item; if (item == null) return false; // a stale key matches nothing (except itself) return this._hashCode == key._hashCode && Object.Equals(item, key.Item); } return false; } public override int GetHashCode() { return _hashCode; } private WeakReference _item; private int _hashCode; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------------------- // // // Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // This file defines a class that holds a weak reference to an object. It preserves the hashcode // of the object and is intended to be used as a key in hashtables or dictionaries. // //------------------------------------------------------------------------------------- using System; using MS.Internal; namespace MS.Internal.Utility { ////// Helper class that allows using a weak reference to an item as a key in a hash table. /// internal class WeakReferenceKey{ public WeakReferenceKey(T item) { Invariant.Assert(item != null); _item = new WeakReference(item); _hashCode = item.GetHashCode(); } public T Item { get { return (T)_item.Target; } } public override bool Equals(object o) { if (o == this) return true; WeakReferenceKey key = o as WeakReferenceKey ; if (key != null) { T item = this.Item; if (item == null) return false; // a stale key matches nothing (except itself) return this._hashCode == key._hashCode && Object.Equals(item, key.Item); } return false; } public override int GetHashCode() { return _hashCode; } private WeakReference _item; private int _hashCode; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- InfoCardServiceInstallComponent.cs
- CopyNamespacesAction.cs
- AlphaSortedEnumConverter.cs
- XmlTextReader.cs
- BooleanAnimationBase.cs
- DataControlField.cs
- TableAutomationPeer.cs
- httpserverutility.cs
- XappLauncher.cs
- AssemblySettingAttributes.cs
- EmissiveMaterial.cs
- _SslStream.cs
- SettingsPropertyValue.cs
- CodeVariableDeclarationStatement.cs
- DataComponentMethodGenerator.cs
- DataListCommandEventArgs.cs
- MouseGesture.cs
- ContextStaticAttribute.cs
- SoapAttributeOverrides.cs
- TextMarkerSource.cs
- ParseHttpDate.cs
- SystemTcpStatistics.cs
- EllipseGeometry.cs
- QilSortKey.cs
- BindingMemberInfo.cs
- EventWaitHandleSecurity.cs
- DateTimeFormatInfo.cs
- HtmlFormAdapter.cs
- UnsafeNativeMethods.cs
- HttpWebResponse.cs
- NetNamedPipeSecurity.cs
- XamlSerializerUtil.cs
- NoClickablePointException.cs
- SystemInfo.cs
- MsdtcWrapper.cs
- ButtonColumn.cs
- Utils.cs
- CryptoStream.cs
- SourceInterpreter.cs
- ListViewGroupConverter.cs
- HierarchicalDataSourceDesigner.cs
- DynamicMethod.cs
- SingleAnimation.cs
- TabControl.cs
- SqlMetaData.cs
- HttpPostedFile.cs
- WebPageTraceListener.cs
- DiscardableAttribute.cs
- DataMember.cs
- _NestedMultipleAsyncResult.cs
- AtomMaterializerLog.cs
- CollectionViewGroupRoot.cs
- CreateUserWizardStep.cs
- TagPrefixCollection.cs
- DebugController.cs
- GraphicsContext.cs
- ipaddressinformationcollection.cs
- Boolean.cs
- TextElementEnumerator.cs
- TCPClient.cs
- PresentationSource.cs
- NullableLongAverageAggregationOperator.cs
- HttpContext.cs
- SignatureToken.cs
- QilList.cs
- FileLogRecordEnumerator.cs
- XsdCachingReader.cs
- GenerateScriptTypeAttribute.cs
- PeerReferralPolicy.cs
- ExpandoObject.cs
- OleDbParameterCollection.cs
- TemplateEditingService.cs
- CellConstant.cs
- ImageMetadata.cs
- SafePEFileHandle.cs
- EncryptedKey.cs
- RuntimeEnvironment.cs
- Policy.cs
- ProcessInfo.cs
- Menu.cs
- HostingEnvironment.cs
- ErrorWebPart.cs
- SiteMembershipCondition.cs
- DataRowCollection.cs
- RegisteredHiddenField.cs
- ErrorHandler.cs
- UndoUnit.cs
- OrderedDictionary.cs
- EventSourceCreationData.cs
- ConnectionConsumerAttribute.cs
- UIElementCollection.cs
- ConfigurationValidatorBase.cs
- TransportSecurityHelpers.cs
- ValidationErrorCollection.cs
- DesignerCatalogPartChrome.cs
- HtmlTitle.cs
- OdbcConnectionString.cs
- TemplateControlCodeDomTreeGenerator.cs
- CompilerResults.cs
- EventDescriptorCollection.cs