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
- SessionEndingEventArgs.cs
- DynamicResourceExtension.cs
- CodeTypeParameter.cs
- HTTPNotFoundHandler.cs
- LicenseManager.cs
- ChannelAcceptor.cs
- FormsAuthentication.cs
- ExtensionWindowHeader.cs
- HttpRuntimeSection.cs
- HttpModuleCollection.cs
- _SingleItemRequestCache.cs
- TypeReference.cs
- DataFieldConverter.cs
- ProfessionalColorTable.cs
- RadialGradientBrush.cs
- RuntimeWrappedException.cs
- DesigntimeLicenseContextSerializer.cs
- OleAutBinder.cs
- WindowsAuthenticationModule.cs
- PolyLineSegment.cs
- CompositionDesigner.cs
- NetworkInformationException.cs
- PopupEventArgs.cs
- XamlSerializationHelper.cs
- infer.cs
- WrappedKeySecurityTokenParameters.cs
- FlowchartDesigner.Helpers.cs
- HighContrastHelper.cs
- JsonSerializer.cs
- BindingContext.cs
- DataColumnCollection.cs
- SymbolTable.cs
- COSERVERINFO.cs
- SessionStateUtil.cs
- NavigationEventArgs.cs
- BitmapEffectInput.cs
- SecurityException.cs
- RowUpdatedEventArgs.cs
- RoutedEventValueSerializer.cs
- ImageListImage.cs
- ComponentSerializationService.cs
- FormsAuthenticationEventArgs.cs
- PageAdapter.cs
- SymbolTable.cs
- OutputCache.cs
- ExceptionRoutedEventArgs.cs
- PerformanceCountersBase.cs
- DebugInfoExpression.cs
- EventProperty.cs
- MouseOverProperty.cs
- DocumentPageViewAutomationPeer.cs
- UInt32.cs
- HttpModuleCollection.cs
- StickyNoteContentControl.cs
- CFStream.cs
- BrowserDefinitionCollection.cs
- ConnectionPoint.cs
- PropertyInfo.cs
- SchemaImporterExtension.cs
- MultiBinding.cs
- SystemSounds.cs
- DataGridPagingPage.cs
- DependencyPropertyKey.cs
- SvcMapFileSerializer.cs
- WasHttpHandlersInstallComponent.cs
- MemberExpression.cs
- DatePickerAutomationPeer.cs
- GroupBox.cs
- TextParagraphCache.cs
- Workspace.cs
- TrustLevel.cs
- StylusPointProperties.cs
- SqlDataReaderSmi.cs
- ReaderWriterLock.cs
- DataRelationPropertyDescriptor.cs
- PropertyGridEditorPart.cs
- FileDialog_Vista.cs
- Decorator.cs
- ClientSettings.cs
- TdsParameterSetter.cs
- ProcessManager.cs
- DataView.cs
- CreateUserWizard.cs
- IDReferencePropertyAttribute.cs
- ThreadAttributes.cs
- XmlNamedNodeMap.cs
- StandardRuntimeEnumValidator.cs
- DataBindingCollectionEditor.cs
- TextServicesProperty.cs
- ViewManager.cs
- MailMessageEventArgs.cs
- TemplateKeyConverter.cs
- DoubleAnimationClockResource.cs
- Internal.cs
- CatalogZoneBase.cs
- SqlTopReducer.cs
- XmlSerializerFactory.cs
- QilValidationVisitor.cs
- PermissionToken.cs
- UInt16.cs