Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Base / MS / Internal / WeakReferenceKey.cs / 1 / 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
- DynamicResourceExtension.cs
- XamlTemplateSerializer.cs
- SHA1.cs
- MenuAutoFormat.cs
- Label.cs
- TimerEventSubscription.cs
- HashRepartitionStream.cs
- NameValueFileSectionHandler.cs
- StringFreezingAttribute.cs
- FusionWrap.cs
- PropertyDescriptor.cs
- UnsafeNativeMethods.cs
- PointCollectionValueSerializer.cs
- MenuItemStyle.cs
- RemoteWebConfigurationHostServer.cs
- ReceiveActivityValidator.cs
- Stack.cs
- DrawingContextDrawingContextWalker.cs
- MessageQueueAccessControlEntry.cs
- XmlNamespaceMappingCollection.cs
- CodeVariableReferenceExpression.cs
- CodeTypeReferenceCollection.cs
- PageThemeParser.cs
- COM2IDispatchConverter.cs
- Input.cs
- ThicknessAnimationUsingKeyFrames.cs
- ExpressionTable.cs
- MenuRendererStandards.cs
- ImageIndexConverter.cs
- AuthenticationManager.cs
- XmlRawWriterWrapper.cs
- DelegatingHeader.cs
- NamespaceExpr.cs
- GuidelineCollection.cs
- SettingsPropertyValueCollection.cs
- DllNotFoundException.cs
- TextViewDesigner.cs
- EntityDataSourceEntitySetNameItem.cs
- ZipPackage.cs
- PrimaryKeyTypeConverter.cs
- BrowserDefinitionCollection.cs
- FlowDocumentScrollViewerAutomationPeer.cs
- TypeConverterHelper.cs
- CaseCqlBlock.cs
- XslAst.cs
- EntityDesignerBuildProvider.cs
- PolyLineSegment.cs
- KeyValueInternalCollection.cs
- BooleanToVisibilityConverter.cs
- Oid.cs
- IndexedDataBuffer.cs
- TimeSpanStorage.cs
- TagPrefixAttribute.cs
- SortKey.cs
- IsolatedStorageFilePermission.cs
- SemanticBasicElement.cs
- BitmapDownload.cs
- MatrixConverter.cs
- TypographyProperties.cs
- DoubleCollectionValueSerializer.cs
- MappingSource.cs
- ControlIdConverter.cs
- QueryStringParameter.cs
- XamlSerializationHelper.cs
- AnnotationHelper.cs
- RuleSetDialog.cs
- FunctionParameter.cs
- ProxyAttribute.cs
- RectangleHotSpot.cs
- ValueExpressions.cs
- EntityFunctions.cs
- AssemblyResourceLoader.cs
- UnionCodeGroup.cs
- BooleanStorage.cs
- DataServiceResponse.cs
- sqlinternaltransaction.cs
- DaylightTime.cs
- HitTestDrawingContextWalker.cs
- FlowDocumentReader.cs
- State.cs
- ProcessThreadCollection.cs
- CircleHotSpot.cs
- NamedPermissionSet.cs
- ImagingCache.cs
- WizardPanelChangingEventArgs.cs
- IfAction.cs
- FixedFlowMap.cs
- Rijndael.cs
- CompilationUtil.cs
- DNS.cs
- HandleRef.cs
- AsyncOperationLifetimeManager.cs
- HybridDictionary.cs
- BindToObject.cs
- PrintEvent.cs
- HttpFormatExtensions.cs
- HttpProfileGroupBase.cs
- GacUtil.cs
- LineSegment.cs
- MatrixUtil.cs