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.
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Runtime.InteropServices;
13 using System.Runtime.CompilerServices;
14 using Internal.Runtime.CompilerServices;
16 namespace System.Runtime.InteropServices.WindowsRuntime
18 internal delegate T Indexer_Get_Delegate<out T>(int index);
20 // This is a set of stub methods implementing the support for the IReadOnlyList`1 interface on WinRT
21 // objects that support IVectorView`1. Used by the interop mashaling infrastructure.
23 // The methods on this class must be written VERY carefully to avoid introducing security holes.
24 // That's because they are invoked with special "this"! The "this" object
25 // for all of these methods are not IVectorViewToIReadOnlyListAdapter objects. Rather, they are of type
26 // IVectorView<T>. No actual IVectorViewToIReadOnlyListAdapter object is ever instantiated. Thus, you will see
27 // a lot of expressions that cast "this" to "IVectorView<T>".
28 [DebuggerDisplay("Count = {Count}")]
29 internal sealed class IVectorViewToIReadOnlyListAdapter
31 private IVectorViewToIReadOnlyListAdapter()
33 Debug.Fail("This class is never instantiated");
36 // T this[int index] { get }
37 internal T Indexer_Get<T>(int index)
40 throw new ArgumentOutOfRangeException(nameof(index));
42 IVectorView<T> _this = Unsafe.As<IVectorView<T>>(this);
46 return _this.GetAt((uint)index);
48 // We delegate bounds checking to the underlying collection and if it detected a fault,
49 // we translate it to the right exception:
53 if (HResults.E_BOUNDS == ex.HResult)
54 throw new ArgumentOutOfRangeException(nameof(index));
60 // T this[int index] { get }
61 internal T Indexer_Get_Variance<T>(int index) where T : class
64 Delegate target = System.StubHelpers.StubHelpers.GetTargetForAmbiguousVariantCall(
66 typeof(IReadOnlyList<T>).TypeHandle.Value,
71 return (Unsafe.As<Indexer_Get_Delegate<T>>(target))(index);
76 return Unsafe.As<T>(Indexer_Get<string>(index));
79 return Indexer_Get<T>(index);