3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>[....]</OWNER>
11 using System.Security;
12 using System.Reflection;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Collections.ObjectModel;
16 using System.Diagnostics.Contracts;
17 using System.Runtime.InteropServices;
18 using System.Runtime.CompilerServices;
20 namespace System.Runtime.InteropServices.WindowsRuntime
22 /// A Windows Runtime IBindableVectorView implementation that wraps around a managed IList exposing
23 /// it to Windows runtime interop.
24 internal sealed class ListToBindableVectorViewAdapter : IBindableVectorView
26 private readonly IList list;
28 internal ListToBindableVectorViewAdapter(IList list)
31 throw new ArgumentNullException("list");
33 Contract.EndContractBlock();
38 private static void EnsureIndexInt32(uint index, int listCapacity)
40 // We use '<=' and not '<' becasue Int32.MaxValue == index would imply
41 // that Size > Int32.MaxValue:
42 if (((uint)Int32.MaxValue) <= index || index >= (uint)listCapacity)
44 Exception e = new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_IndexLargerThanMaxValue"));
45 e.SetErrorCode(__HResults.E_BOUNDS);
50 // IBindableIterable implementation:
52 public IBindableIterator First()
54 IEnumerator enumerator = list.GetEnumerator();
55 return new EnumeratorToIteratorAdapter<object>(new EnumerableToBindableIterableAdapter.NonGenericToGenericEnumerator(enumerator));
58 // IBindableVectorView implementation:
60 public object GetAt(uint index)
62 EnsureIndexInt32(index, list.Count);
66 return list[(int)index];
69 catch (ArgumentOutOfRangeException ex)
71 throw WindowsRuntimeMarshal.GetExceptionForHR(__HResults.E_BOUNDS, ex, "ArgumentOutOfRange_IndexOutOfRange");
79 return (uint)list.Count;
83 public bool IndexOf(object value, out uint index)
85 int ind = list.IndexOf(value);