// 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.Security; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; namespace System.Runtime.InteropServices.WindowsRuntime { // These stubs will be used when a call via IReadOnlyCollection> is made in managed code. // This can mean two things - either the underlying unmanaged object implements IMapView or it // implements IVectorView> and we cannot determine this statically in the general // case so we have to cast at run-time. Used by the interop mashaling infrastructure. // // The methods on this class must be written VERY carefully to avoid introducing security holes. // That's because they are invoked with special "this"! The "this" object // for all of these methods are not MapViewToReadOnlyCollectionAdapter objects. Rather, they are of type // IVectorView> or IMapView. No actual MapViewToReadOnlyCollectionAdapter object is ever // instantiated. Thus, you will see a lot of expressions that cast "this" to "IVectorView>" // or "IMapView". internal sealed class MapViewToReadOnlyCollectionAdapter { private MapViewToReadOnlyCollectionAdapter() { Debug.Assert(false, "This class is never instantiated"); } // int Count { get } [Pure] internal int Count() { object _this = JitHelpers.UnsafeCast(this); IMapView _this_map = _this as IMapView; if (_this_map != null) { uint size = _this_map.Size; if (((uint)Int32.MaxValue) < size) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingDictionaryTooLarge")); } return (int)size; } else { IVectorView> _this_vector = JitHelpers.UnsafeCast>>(this); uint size = _this_vector.Size; if (((uint)Int32.MaxValue) < size) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge")); } return (int)size; } } } }