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.Diagnostics;
7 using Internal.Runtime.CompilerServices;
9 namespace System.Runtime.InteropServices.WindowsRuntime
11 // This is a set of stub methods implementing the support for the IList`1 interface on WinRT
12 // objects that support IVector`1. Used by the interop mashaling infrastructure.
14 // The methods on this class must be written VERY carefully to avoid introducing security holes.
15 // That's because they are invoked with special "this"! The "this" object
16 // for all of these methods are not VectorToListAdapter objects. Rather, they are of type
17 // IVector<T>. No actual VectorToListAdapter object is ever instantiated. Thus, you will see
18 // a lot of expressions that cast "this" to "IVector<T>".
19 internal sealed class VectorToListAdapter
21 private VectorToListAdapter()
23 Debug.Fail("This class is never instantiated");
26 // T this[int index] { get }
27 internal T Indexer_Get<T>(int index)
30 throw new ArgumentOutOfRangeException(nameof(index));
32 IVector<T> _this = Unsafe.As<IVector<T>>(this);
33 return GetAt(_this, (uint)index);
36 // T this[int index] { set }
37 internal void Indexer_Set<T>(int index, T value)
40 throw new ArgumentOutOfRangeException(nameof(index));
42 IVector<T> _this = Unsafe.As<IVector<T>>(this);
43 SetAt(_this, (uint)index, value);
46 // int IndexOf(T item)
47 internal int IndexOf<T>(T item)
49 IVector<T> _this = Unsafe.As<IVector<T>>(this);
52 bool exists = _this.IndexOf(item, out index);
57 if (((uint)int.MaxValue) < index)
59 throw new InvalidOperationException(SR.InvalidOperation_CollectionBackingListTooLarge);
65 // void Insert(int index, T item)
66 internal void Insert<T>(int index, T item)
69 throw new ArgumentOutOfRangeException(nameof(index));
71 IVector<T> _this = Unsafe.As<IVector<T>>(this);
72 InsertAtHelper<T>(_this, (uint)index, item);
75 // void RemoveAt(int index)
76 internal void RemoveAt<T>(int index)
79 throw new ArgumentOutOfRangeException(nameof(index));
81 IVector<T> _this = Unsafe.As<IVector<T>>(this);
82 RemoveAtHelper<T>(_this, (uint)index);
87 internal static T GetAt<T>(IVector<T> _this, uint index)
91 return _this.GetAt(index);
93 // We delegate bounds checking to the underlying collection and if it detected a fault,
94 // we translate it to the right exception:
98 if (HResults.E_BOUNDS == ex.HResult)
99 throw new ArgumentOutOfRangeException(nameof(index));
105 private static void SetAt<T>(IVector<T> _this, uint index, T value)
109 _this.SetAt(index, value);
111 // We deligate bounds checking to the underlying collection and if it detected a fault,
112 // we translate it to the right exception:
116 if (HResults.E_BOUNDS == ex.HResult)
117 throw new ArgumentOutOfRangeException(nameof(index));
123 private static void InsertAtHelper<T>(IVector<T> _this, uint index, T item)
127 _this.InsertAt(index, item);
129 // We delegate bounds checking to the underlying collection and if it detected a fault,
130 // we translate it to the right exception:
134 if (HResults.E_BOUNDS == ex.HResult)
135 throw new ArgumentOutOfRangeException(nameof(index));
141 internal static void RemoveAtHelper<T>(IVector<T> _this, uint index)
145 _this.RemoveAt(index);
147 // We delegate bounds checking to the underlying collection and if it detected a fault,
148 // we translate it to the right exception:
152 if (HResults.E_BOUNDS == ex.HResult)
153 throw new ArgumentOutOfRangeException(nameof(index));