// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; using System.Runtime.InteropServices.WindowsRuntime; namespace System.Runtime.InteropServices.WindowsRuntime { [Serializable] [DebuggerDisplay("Count = {Count}")] internal sealed class DictionaryValueCollection : ICollection { private readonly IDictionary dictionary; public DictionaryValueCollection(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException(nameof(dictionary)); this.dictionary = dictionary; } public void CopyTo(TValue[] array, int index) { if (array == null) throw new ArgumentNullException(nameof(array)); if (index < 0) throw new ArgumentOutOfRangeException(nameof(index)); if (array.Length <= index && this.Count > 0) throw new ArgumentException(Environment.GetResourceString("Arg_IndexOutOfRangeException")); if (array.Length - index < dictionary.Count) throw new ArgumentException(Environment.GetResourceString("Argument_InsufficientSpaceToCopyCollection")); int i = index; foreach (KeyValuePair mapping in dictionary) { array[i++] = mapping.Value; } } public int Count { get { return dictionary.Count; } } bool ICollection.IsReadOnly { get { return true; } } void ICollection.Add(TValue item) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ValueCollectionSet")); } void ICollection.Clear() { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ValueCollectionSet")); } public bool Contains(TValue item) { EqualityComparer comparer = EqualityComparer.Default; foreach (TValue value in this) if (comparer.Equals(item, value)) return true; return false; } bool ICollection.Remove(TValue item) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_ValueCollectionSet")); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)this).GetEnumerator(); } public IEnumerator GetEnumerator() { return new DictionaryValueEnumerator(dictionary); } } // public class DictionaryValueCollection [Serializable] internal sealed class DictionaryValueEnumerator : IEnumerator { private readonly IDictionary dictionary; private IEnumerator> enumeration; public DictionaryValueEnumerator(IDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException(nameof(dictionary)); this.dictionary = dictionary; this.enumeration = dictionary.GetEnumerator(); } void IDisposable.Dispose() { enumeration.Dispose(); } public bool MoveNext() { return enumeration.MoveNext(); } Object IEnumerator.Current { get { return ((IEnumerator)this).Current; } } public TValue Current { get { return enumeration.Current.Value; } } public void Reset() { enumeration = dictionary.GetEnumerator(); } } // class DictionaryValueEnumerator } // DictionaryValueCollection.cs