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.Collections;
8 namespace System.Runtime.InteropServices.WindowsRuntime
10 /// A Windows Runtime IBindableVectorView implementation that wraps around a managed IList exposing
11 /// it to Windows runtime interop.
12 internal sealed class ListToBindableVectorViewAdapter : IBindableVectorView
14 private readonly IList list;
16 internal ListToBindableVectorViewAdapter(IList list)
19 throw new ArgumentNullException(nameof(list));
25 private static void EnsureIndexInt32(uint index, int listCapacity)
27 // We use '<=' and not '<' becasue int.MaxValue == index would imply
28 // that Size > int.MaxValue:
29 if (((uint)int.MaxValue) <= index || index >= (uint)listCapacity)
31 Exception e = new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_IndexLargerThanMaxValue);
32 e.HResult = HResults.E_BOUNDS;
37 // IBindableIterable implementation:
39 public IBindableIterator First()
41 IEnumerator enumerator = list.GetEnumerator();
42 return new EnumeratorToIteratorAdapter<object?>(new EnumerableToBindableIterableAdapter.NonGenericToGenericEnumerator(enumerator));
45 // IBindableVectorView implementation:
47 public object? GetAt(uint index)
49 EnsureIndexInt32(index, list.Count);
53 return list[(int)index];
55 catch (ArgumentOutOfRangeException ex)
57 throw WindowsRuntimeMarshal.GetExceptionForHR(HResults.E_BOUNDS, ex, "ArgumentOutOfRange_IndexOutOfRange");
65 return (uint)list.Count;
69 public bool IndexOf(object value, out uint index)
71 int ind = list.IndexOf(value);