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
- ActiveXHelper.cs
- MissingManifestResourceException.cs
- AssociationEndMember.cs
- CompositeTypefaceMetrics.cs
- BevelBitmapEffect.cs
- ReturnEventArgs.cs
- ReadOnlyCollection.cs
- TextElementCollectionHelper.cs
- ComponentCodeDomSerializer.cs
- Span.cs
- FormViewDeleteEventArgs.cs
- ConfigUtil.cs
- FormatConvertedBitmap.cs
- SymmetricCryptoHandle.cs
- WarningException.cs
- EmptyStringExpandableObjectConverter.cs
- TrackingValidationObjectDictionary.cs
- DataMemberFieldEditor.cs
- TextServicesCompartment.cs
- Decoder.cs
- XPathAxisIterator.cs
- SchemaImporterExtension.cs
- OleDbDataAdapter.cs
- HttpRawResponse.cs
- Int32RectConverter.cs
- ScriptComponentDescriptor.cs
- XmlSchemaSimpleTypeUnion.cs
- DbModificationClause.cs
- TaiwanLunisolarCalendar.cs
- KeyGestureConverter.cs
- TreeNode.cs
- ViewCellSlot.cs
- ProfileGroupSettings.cs
- PageContentCollection.cs
- CallContext.cs
- SafeMarshalContext.cs
- HttpUnhandledOperationInvoker.cs
- SimpleModelProvider.cs
- MsdtcWrapper.cs
- RegexStringValidator.cs
- PerformanceCounter.cs
- DependencyPropertyChangedEventArgs.cs
- ToolStripGripRenderEventArgs.cs
- ArcSegment.cs
- BitmapEffect.cs
- DPTypeDescriptorContext.cs
- AttachedProperty.cs
- TableLayoutSettingsTypeConverter.cs
- TraceUtility.cs
- AdornedElementPlaceholder.cs
- ClientTargetCollection.cs
- IdnElement.cs
- ObjRef.cs
- XmlSchemaObjectTable.cs
- TypedDataSetSchemaImporterExtension.cs
- ServiceRoute.cs
- CompModHelpers.cs
- MsmqIntegrationElement.cs
- LabelEditEvent.cs
- DefaultValueTypeConverter.cs
- WorkflowDesigner.cs
- AnnotationStore.cs
- UnsafeNativeMethods.cs
- File.cs
- Lease.cs
- BindableTemplateBuilder.cs
- WebPartZone.cs
- ProgressiveCrcCalculatingStream.cs
- SqlDataSourceCommandEventArgs.cs
- HTTPAPI_VERSION.cs
- CacheChildrenQuery.cs
- HttpListenerException.cs
- SafeNativeMethods.cs
- sitestring.cs
- SqlFacetAttribute.cs
- RegexCompilationInfo.cs
- DbgUtil.cs
- WinOEToolBoxItem.cs
- JavaScriptString.cs
- PerformanceCounterLib.cs
- ViewBox.cs
- Int32Collection.cs
- ActivityDesignerResources.cs
- TraceSwitch.cs
- FileDetails.cs
- DesignerFrame.cs
- ApplicationGesture.cs
- ProxySimple.cs
- DesignerActionUIStateChangeEventArgs.cs
- LoginView.cs
- HttpClientCertificate.cs
- JournalEntryStack.cs
- MtomMessageEncodingBindingElement.cs
- Package.cs
- ItemContainerGenerator.cs
- Publisher.cs
- IODescriptionAttribute.cs
- DataGridViewCellParsingEventArgs.cs
- Itemizer.cs
- XmlSchemaSequence.cs