1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using Internal.Runtime.CompilerServices;
10 namespace System.Runtime.InteropServices.WindowsRuntime
12 // These stubs will be used when a call via IReadOnlyCollection<KeyValuePair<K, V>> is made in managed code.
13 // This can mean two things - either the underlying unmanaged object implements IMapView<K, V> or it
14 // implements IVectorView<IKeyValuePair<K, V>> and we cannot determine this statically in the general
15 // case so we have to cast at run-time. Used by the interop mashaling infrastructure.
17 // The methods on this class must be written VERY carefully to avoid introducing security holes.
18 // That's because they are invoked with special "this"! The "this" object
19 // for all of these methods are not MapViewToReadOnlyCollectionAdapter objects. Rather, they are of type
20 // IVectorView<KeyValuePair<K, V>> or IMapView<K, V>. No actual MapViewToReadOnlyCollectionAdapter object is ever
21 // instantiated. Thus, you will see a lot of expressions that cast "this" to "IVectorView<KeyValuePair<K, V>>"
22 // or "IMapView<K, V>".
23 internal sealed class MapViewToReadOnlyCollectionAdapter
25 private MapViewToReadOnlyCollectionAdapter()
27 Debug.Fail("This class is never instantiated");
31 internal int Count<K, V>()
33 object _this = Unsafe.As<object>(this);
35 if (_this is IMapView<K, V> _this_map)
37 uint size = _this_map.Size;
39 if (((uint)int.MaxValue) < size)
41 throw new InvalidOperationException(SR.InvalidOperation_CollectionBackingDictionaryTooLarge);
48 IVectorView<KeyValuePair<K, V>> _this_vector = Unsafe.As<IVectorView<KeyValuePair<K, V>>>(this);
49 uint size = _this_vector.Size;
51 if (((uint)int.MaxValue) < size)
53 throw new InvalidOperationException(SR.InvalidOperation_CollectionBackingListTooLarge);