Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / System / Collections / ObjectModel / WeakReadOnlyCollection.cs / 1305600 / WeakReadOnlyCollection.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// This is basically copied from ReadOnlyCollection, with just enough modification
// to support storage of items as WeakReferences. Use of internal BCL features
// is commented out.
// Most of the O(N) operations - Contains, IndexOf, etc. -
// are implemented by simply exploding the base list into a List. This isn't
// particularly efficient, but we don't expect to use these methods very often.
// The important scenario is one where we need WR not because we expect the targets
// to die (we don't), but because we need to avoid creating cycles containing the
// WeakReadOnlyCollection. Thus we don't check for WR.Target==null.
namespace System.Collections.ObjectModel
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using MS.Internal.WindowsBase; // [FriendAccessAllowed]
[Serializable]
[System.Runtime.InteropServices.ComVisible(false)]
//[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[FriendAccessAllowed]
internal class WeakReadOnlyCollection: IList, IList
{
//IList list;
IList list;
[NonSerialized]
private Object _syncRoot;
public WeakReadOnlyCollection(IList list) { // assumption: the WRs in list refer to T's
if (list == null) {
throw new ArgumentNullException("list");
}
this.list = list;
}
public int Count {
get { return list.Count; }
}
public T this[int index] {
//get { return list[index]; }
get { return (T)list[index].Target; }
}
public bool Contains(T value) {
//return list.Contains(value);
return CreateDereferencedList().Contains(value);
}
public void CopyTo(T[] array, int index) {
//list.CopyTo(array, index);
CreateDereferencedList().CopyTo(array, index);
}
public IEnumerator GetEnumerator() {
//return list.GetEnumerator();
return new WeakEnumerator(list.GetEnumerator());
}
public int IndexOf(T value) {
//return list.IndexOf(value);
return CreateDereferencedList().IndexOf(value);
}
/*
protected IList Items {
get {
return list;
}
}
*/
bool ICollection.IsReadOnly {
get { return true; }
}
T IList.this[int index] {
//get { return list[index]; }
get { return (T)list[index].Target; }
set {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
}
}
void ICollection.Add(T value) {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
}
void ICollection.Clear() {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
}
void IList.Insert(int index, T value) {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
}
bool ICollection.Remove(T value) {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
//return false;
}
void IList.RemoveAt(int index) {
//ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
throw new NotSupportedException(SR.Get(SRID.NotSupported_ReadOnlyCollection));
}
IEnumerator IEnumerable.GetEnumerator() {
//return ((IEnumerable)list).GetEnumerator();
return new WeakEnumerator(((IEnumerable)list).GetEnumerator());
}
bool ICollection.IsSynchronized {
get { return false; }
}
object ICollection.SyncRoot {
get {
if( _syncRoot == null) {
ICollection c = list as ICollection;
if( c != null) {
_syncRoot = c.SyncRoot;
}
else {
System.Threading.Interlocked.CompareExchange
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DocumentCollection.cs
- SatelliteContractVersionAttribute.cs
- SetIterators.cs
- QueryExecutionOption.cs
- ExpressionBuilderContext.cs
- SafeEventLogReadHandle.cs
- SessionIDManager.cs
- DynamicValueConverter.cs
- RSAPKCS1SignatureDeformatter.cs
- GroupQuery.cs
- ProtocolElement.cs
- ResumeStoryboard.cs
- SafeLibraryHandle.cs
- BaseDataBoundControl.cs
- SchemaSetCompiler.cs
- WorkflowMessageEventHandler.cs
- Int32Animation.cs
- SrgsSemanticInterpretationTag.cs
- GridViewColumnCollection.cs
- HtmlTableCellCollection.cs
- _CommandStream.cs
- CompareValidator.cs
- EntityDataSourceStatementEditorForm.cs
- FillRuleValidation.cs
- _ServiceNameStore.cs
- ObservableDictionary.cs
- AuthenticationServiceManager.cs
- CharacterHit.cs
- CurrentTimeZone.cs
- XXXOnTypeBuilderInstantiation.cs
- GiveFeedbackEvent.cs
- PeerInputChannel.cs
- _ScatterGatherBuffers.cs
- ReadOnlyPropertyMetadata.cs
- DataGridViewDesigner.cs
- HttpCookie.cs
- EnumUnknown.cs
- DataGridViewSortCompareEventArgs.cs
- LinkButton.cs
- LogWriteRestartAreaAsyncResult.cs
- MultiByteCodec.cs
- MetadataArtifactLoaderFile.cs
- WindowsFormsHelpers.cs
- SafeReversePInvokeHandle.cs
- XNodeSchemaApplier.cs
- DecoderBestFitFallback.cs
- ReferenceService.cs
- WsatAdminException.cs
- RegexNode.cs
- CaseInsensitiveComparer.cs
- SQLRoleProvider.cs
- GroupBoxAutomationPeer.cs
- Empty.cs
- XsdValidatingReader.cs
- D3DImage.cs
- CustomErrorCollection.cs
- DrawingContextDrawingContextWalker.cs
- IEnumerable.cs
- HttpApplicationFactory.cs
- ColorPalette.cs
- GridViewCancelEditEventArgs.cs
- MainMenu.cs
- RowToParametersTransformer.cs
- XmlAttributeAttribute.cs
- ReflectionHelper.cs
- MexTcpBindingElement.cs
- ToolboxItemCollection.cs
- GridViewColumnCollectionChangedEventArgs.cs
- Camera.cs
- EncoderParameters.cs
- HebrewNumber.cs
- XmlDataDocument.cs
- StateMachineExecutionState.cs
- DirectionalLight.cs
- SafeSystemMetrics.cs
- XmlCountingReader.cs
- ApplicationManager.cs
- XhtmlBasicTextViewAdapter.cs
- DependencyObjectProvider.cs
- _NtlmClient.cs
- SplitterCancelEvent.cs
- PhoneCall.cs
- CompatibleComparer.cs
- RawContentTypeMapper.cs
- PointValueSerializer.cs
- ConfigXmlComment.cs
- EventMappingSettingsCollection.cs
- ColorMap.cs
- ReferenceConverter.cs
- milexports.cs
- DBConcurrencyException.cs
- MachineKeyConverter.cs
- WebBrowserNavigatingEventHandler.cs
- safelinkcollection.cs
- MemoryMappedView.cs
- StaticExtension.cs
- FormViewDeleteEventArgs.cs
- DataGridTablesFactory.cs
- PeerInputChannel.cs
- ReaderWriterLockSlim.cs