Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Statements / CasesDictionary.cs / 1305376 / CasesDictionary.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities.Statements
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
class CasesDictionary : IDictionary
{
bool isNullKeyPresent;
TValue nullKeyValue;
IDictionary innerDictionary;
public CasesDictionary()
: base()
{
this.innerDictionary = new Dictionary();
}
public int Count
{
get { return this.innerDictionary.Count + (this.isNullKeyPresent ? 1 : 0); }
}
public bool IsReadOnly
{
get { return false; }
}
public ICollection Keys
{
get
{
return new NullKeyDictionaryKeyCollection(this);
}
}
public ICollection Values
{
get { return new NullKeyDictionaryValueCollection(this); }
}
public TValue this[TKey key]
{
get
{
if (key == null)
{
if (this.isNullKeyPresent)
{
return this.nullKeyValue;
}
else
{
throw FxTrace.Exception.AsError(new KeyNotFoundException());
}
}
else
{
return this.innerDictionary[key];
}
}
set
{
if (key == null)
{
this.isNullKeyPresent = true;
this.nullKeyValue = value;
}
else
{
this.innerDictionary[key] = value;
}
}
}
public void Add(TKey key, TValue value)
{
if (key == null)
{
if (this.isNullKeyPresent)
{
throw FxTrace.Exception.Argument("key", SR.NullKeyAlreadyPresent);
}
this.isNullKeyPresent = true;
this.nullKeyValue = value;
}
else
{
this.innerDictionary.Add(key, value);
}
}
public bool ContainsKey(TKey key)
{
return key == null ? this.isNullKeyPresent : this.innerDictionary.ContainsKey(key);
}
public bool Remove(TKey key)
{
if (key == null)
{
bool result = this.isNullKeyPresent;
this.isNullKeyPresent = false;
this.nullKeyValue = default(TValue);
return result;
}
else
{
return this.innerDictionary.Remove(key);
}
}
public bool TryGetValue(TKey key, out TValue value)
{
if (key == null)
{
if (this.isNullKeyPresent)
{
value = this.nullKeyValue;
return true;
}
else
{
value = default(TValue);
return false;
}
}
else
{
return this.innerDictionary.TryGetValue(key, out value);
}
}
public void Add(KeyValuePair item)
{
Add(item.Key, item.Value);
}
public void Clear()
{
this.isNullKeyPresent = false;
this.nullKeyValue = default(TValue);
this.innerDictionary.Clear();
}
public bool Contains(KeyValuePair item)
{
if (item.Key == null)
{
if (this.isNullKeyPresent)
{
return item.Value == null ? this.nullKeyValue == null : item.Value.Equals(this.nullKeyValue);
}
else
{
return false;
}
}
else
{
return this.innerDictionary.Contains(item);
}
}
public void CopyTo(KeyValuePair[] array, int arrayIndex)
{
this.innerDictionary.CopyTo(array, arrayIndex);
if (this.isNullKeyPresent)
{
array[arrayIndex + this.innerDictionary.Count] = new KeyValuePair(default(TKey), this.nullKeyValue);
}
}
public bool Remove(KeyValuePair item)
{
if (item.Key == null)
{
if (this.Contains(item))
{
this.isNullKeyPresent = false;
this.nullKeyValue = default(TValue);
return true;
}
else
{
return false;
}
}
else
{
return this.innerDictionary.Remove(item);
}
}
public IEnumerator> GetEnumerator()
{
IEnumerator> innerEnumerator = this.innerDictionary.GetEnumerator() as IEnumerator>;
while (innerEnumerator.MoveNext())
{
yield return innerEnumerator.Current;
}
if (this.isNullKeyPresent)
{
yield return new KeyValuePair(default(TKey), this.nullKeyValue);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable>)this).GetEnumerator();
}
class NullKeyDictionaryKeyCollection : ICollection
{
CasesDictionary nullKeyDictionary;
public NullKeyDictionaryKeyCollection(CasesDictionary nullKeyDictionary)
{
this.nullKeyDictionary = nullKeyDictionary;
}
public int Count
{
get
{
int count = this.nullKeyDictionary.innerDictionary.Keys.Count;
if (this.nullKeyDictionary.isNullKeyPresent)
{
count++;
}
return count;
}
}
public bool IsReadOnly
{
get { return true; }
}
public void Add(TypeKey item)
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.KeyCollectionUpdatesNotAllowed));
}
public void Clear()
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.KeyCollectionUpdatesNotAllowed));
}
public bool Contains(TypeKey item)
{
return item == null ? this.nullKeyDictionary.isNullKeyPresent : this.nullKeyDictionary.innerDictionary.Keys.Contains(item);
}
public void CopyTo(TypeKey[] array, int arrayIndex)
{
this.nullKeyDictionary.innerDictionary.Keys.CopyTo(array, arrayIndex);
if (this.nullKeyDictionary.isNullKeyPresent)
{
array[arrayIndex + this.nullKeyDictionary.innerDictionary.Keys.Count] = default(TypeKey);
}
}
public bool Remove(TypeKey item)
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.KeyCollectionUpdatesNotAllowed));
}
public IEnumerator GetEnumerator()
{
foreach (TypeKey item in this.nullKeyDictionary.innerDictionary.Keys)
{
yield return item;
}
if (this.nullKeyDictionary.isNullKeyPresent)
{
yield return default(TypeKey);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
}
class NullKeyDictionaryValueCollection : ICollection
{
CasesDictionary nullKeyDictionary;
public NullKeyDictionaryValueCollection(CasesDictionary nullKeyDictionary)
{
this.nullKeyDictionary = nullKeyDictionary;
}
public int Count
{
get
{
int count = this.nullKeyDictionary.innerDictionary.Values.Count;
if (this.nullKeyDictionary.isNullKeyPresent)
{
count++;
}
return count;
}
}
public bool IsReadOnly
{
get { return true; }
}
public void Add(TypeValue item)
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.ValueCollectionUpdatesNotAllowed));
}
public void Clear()
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.ValueCollectionUpdatesNotAllowed));
}
public bool Contains(TypeValue item)
{
return this.nullKeyDictionary.innerDictionary.Values.Contains(item) ||
(this.nullKeyDictionary.isNullKeyPresent && this.nullKeyDictionary.nullKeyValue.Equals(item));
}
public void CopyTo(TypeValue[] array, int arrayIndex)
{
this.nullKeyDictionary.innerDictionary.Values.CopyTo(array, arrayIndex);
if (this.nullKeyDictionary.isNullKeyPresent)
{
array[arrayIndex + this.nullKeyDictionary.innerDictionary.Values.Count] = this.nullKeyDictionary.nullKeyValue;
}
}
public bool Remove(TypeValue item)
{
throw FxTrace.Exception.AsError(new NotSupportedException(SR.ValueCollectionUpdatesNotAllowed));
}
public IEnumerator GetEnumerator()
{
foreach (TypeValue item in this.nullKeyDictionary.innerDictionary.Values)
{
yield return item;
}
if (this.nullKeyDictionary.isNullKeyPresent)
{
yield return this.nullKeyDictionary.nullKeyValue;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlSchemaAttributeGroupRef.cs
- Stackframe.cs
- HttpHeaderCollection.cs
- SafeReversePInvokeHandle.cs
- MultiPropertyDescriptorGridEntry.cs
- TriggerActionCollection.cs
- TextFormatterHost.cs
- SectionInput.cs
- ContainerActivationHelper.cs
- Underline.cs
- SiteMapNodeItemEventArgs.cs
- MessageSmuggler.cs
- PackageRelationshipCollection.cs
- DataGridViewColumnDesignTimeVisibleAttribute.cs
- AllMembershipCondition.cs
- IntSumAggregationOperator.cs
- TagNameToTypeMapper.cs
- SecurityHeaderLayout.cs
- ComplexPropertyEntry.cs
- EntityDataSourceColumn.cs
- DataServiceEntityAttribute.cs
- ExpressionLexer.cs
- SystemBrushes.cs
- AutomationPropertyInfo.cs
- BulletedListEventArgs.cs
- WrapperEqualityComparer.cs
- ErrorWebPart.cs
- metadatamappinghashervisitor.hashsourcebuilder.cs
- ButtonBaseAutomationPeer.cs
- GacUtil.cs
- SystemResources.cs
- RubberbandSelector.cs
- ClientSideQueueItem.cs
- CodeDomSerializer.cs
- AssemblyBuilder.cs
- MetadataElement.cs
- CommonProperties.cs
- MailBnfHelper.cs
- ServiceObjectContainer.cs
- PersonalizationStateInfo.cs
- CompilerLocalReference.cs
- CodeTypeParameter.cs
- XPathCompileException.cs
- EntityViewGenerator.cs
- XsltArgumentList.cs
- CodeCommentStatement.cs
- GridView.cs
- TraceSection.cs
- SecurityKeyType.cs
- WebPageTraceListener.cs
- Metafile.cs
- ParamArrayAttribute.cs
- HttpWebRequest.cs
- MessageTransmitTraceRecord.cs
- EnumBuilder.cs
- XmlDataSourceNodeDescriptor.cs
- PolyBezierSegment.cs
- ConfigXmlElement.cs
- WindowsTooltip.cs
- SemaphoreFullException.cs
- Permission.cs
- MatrixCamera.cs
- AttachedPropertyDescriptor.cs
- QueryStringParameter.cs
- OdbcEnvironment.cs
- Rotation3DAnimationUsingKeyFrames.cs
- BindStream.cs
- connectionpool.cs
- PermissionListSet.cs
- ValidationPropertyAttribute.cs
- MappingModelBuildProvider.cs
- ReceiveMessageAndVerifySecurityAsyncResultBase.cs
- diagnosticsswitches.cs
- CryptoApi.cs
- TextDecorationCollectionConverter.cs
- SmiEventSink_Default.cs
- XmlSerializerFormatAttribute.cs
- SettingsPropertyWrongTypeException.cs
- InkCanvasSelection.cs
- StylusButtonEventArgs.cs
- sqlmetadatafactory.cs
- SplitterPanel.cs
- EntityUtil.cs
- InvalidEnumArgumentException.cs
- VisualStateChangedEventArgs.cs
- ParseNumbers.cs
- MailSettingsSection.cs
- SafeNativeMethodsMilCoreApi.cs
- SizeKeyFrameCollection.cs
- Gdiplus.cs
- NativeMethods.cs
- SkinBuilder.cs
- OleDbRowUpdatedEvent.cs
- EarlyBoundInfo.cs
- MatrixTransform.cs
- CompilationLock.cs
- SpecularMaterial.cs
- CqlParser.cs
- RoleManagerEventArgs.cs
- CallInfo.cs