3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>[....]</OWNER>
12 using System.Security;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Diagnostics.Contracts;
16 using System.Runtime.InteropServices;
17 using System.Runtime.CompilerServices;
19 namespace System.Runtime.InteropServices.WindowsRuntime
21 // This is a set of stub methods implementing the support for the IList interface on WinRT
22 // objects that support IBindableVector. Used by the interop mashaling infrastructure.
24 // The methods on this class must be written VERY carefully to avoid introducing security holes.
25 // That's because they are invoked with special "this"! The "this" object
26 // for all of these methods are not BindableVectorToListAdapter objects. Rather, they are
27 // of type IBindableVector. No actual BindableVectorToListAdapter object is ever instantiated.
28 // Thus, you will see a lot of expressions that cast "this" to "IBindableVector".
29 internal sealed class BindableVectorToListAdapter
31 private BindableVectorToListAdapter()
33 Contract.Assert(false, "This class is never instantiated");
36 // object this[int index] { get }
38 internal object Indexer_Get(int index)
41 throw new ArgumentOutOfRangeException("index");
43 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
44 return GetAt(_this, (uint)index);
47 // object this[int index] { set }
49 internal void Indexer_Set(int index, object value)
52 throw new ArgumentOutOfRangeException("index");
54 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
55 SetAt(_this, (uint)index, value);
58 // int Add(object value)
60 internal int Add(object value)
62 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
65 uint size = _this.Size;
66 if (((uint)Int32.MaxValue) < size)
68 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
71 return (int)(size - 1);
74 // bool Contains(object item)
76 internal bool Contains(object item)
78 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
81 return _this.IndexOf(item, out index);
88 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
92 // bool IsFixedSize { get }
95 internal bool IsFixedSize()
100 // bool IsReadOnly { get }
103 internal bool IsReadOnly()
108 // int IndexOf(object item)
110 internal int IndexOf(object item)
112 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
115 bool exists = _this.IndexOf(item, out index);
120 if (((uint)Int32.MaxValue) < index)
122 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
128 // void Insert(int index, object item)
130 internal void Insert(int index, object item)
133 throw new ArgumentOutOfRangeException("index");
135 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
136 InsertAtHelper(_this, (uint)index, item);
139 // bool Remove(object item)
141 internal void Remove(object item)
143 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
146 bool exists = _this.IndexOf(item, out index);
150 if (((uint)Int32.MaxValue) < index)
152 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
155 RemoveAtHelper(_this, index);
159 // void RemoveAt(int index)
161 internal void RemoveAt(int index)
164 throw new ArgumentOutOfRangeException("index");
166 IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
167 RemoveAtHelper(_this, (uint)index);
172 private static object GetAt(IBindableVector _this, uint index)
176 return _this.GetAt(index);
178 // We delegate bounds checking to the underlying collection and if it detected a fault,
179 // we translate it to the right exception:
183 if (__HResults.E_BOUNDS == ex._HResult)
184 throw new ArgumentOutOfRangeException("index");
190 private static void SetAt(IBindableVector _this, uint index, object value)
194 _this.SetAt(index, value);
196 // We delegate bounds checking to the underlying collection and if it detected a fault,
197 // we translate it to the right exception:
201 if (__HResults.E_BOUNDS == ex._HResult)
202 throw new ArgumentOutOfRangeException("index");
208 private static void InsertAtHelper(IBindableVector _this, uint index, object item)
212 _this.InsertAt(index, item);
214 // We delegate bounds checking to the underlying collection and if it detected a fault,
215 // we translate it to the right exception:
219 if (__HResults.E_BOUNDS == ex._HResult)
220 throw new ArgumentOutOfRangeException("index");
226 private static void RemoveAtHelper(IBindableVector _this, uint index)
230 _this.RemoveAt(index);
232 // We delegate bounds checking to the underlying collection and if it detected a fault,
233 // we translate it to the right exception:
237 if (__HResults.E_BOUNDS == ex._HResult)
238 throw new ArgumentOutOfRangeException("index");