blob: 350920564a29bb78521085589e20d6481bcb9a68 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
using System;
using System.Runtime;
using System.Security;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace System.Runtime.InteropServices.WindowsRuntime
{
// This is a set of stub methods implementing the support for the ICollection interface on WinRT
// objects that support IBindableVector. Used by the interop mashaling infrastructure.
//
// The methods on this class must be written VERY carefully to avoid introducing security holes.
// That's because they are invoked with special "this"! The "this" object
// for all of these methods are not BindableVectorToCollectionAdapter objects. Rather, they are
// of type IBindableVector. No actual BindableVectorToCollectionAdapter object is ever instantiated.
// Thus, you will see a lot of expressions that cast "this" to "IBindableVector".
internal sealed class BindableVectorToCollectionAdapter
{
private BindableVectorToCollectionAdapter()
{
Debug.Assert(false, "This class is never instantiated");
}
// int Count { get }
[Pure]
internal int Count()
{
IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
uint size = _this.Size;
if (((uint)Int32.MaxValue) < size)
{
throw new InvalidOperationException(SR.InvalidOperation_CollectionBackingListTooLarge);
}
return (int)size;
}
// bool IsSynchronized { get }
[Pure]
internal bool IsSynchronized()
{
return false;
}
// object SyncRoot { get }
[Pure]
internal object SyncRoot()
{
return this;
}
// void CopyTo(Array array, int index)
[Pure]
internal void CopyTo(Array array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
// ICollection expects the destination array to be single-dimensional.
if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
int destLB = array.GetLowerBound(0);
int srcLen = Count();
int destLen = array.GetLength(0);
if (arrayIndex < destLB)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
// Does the dimension in question have sufficient space to copy the expected number of entries?
// We perform this check before valid index check to ensure the exception message is in sync with
// the following snippet that uses regular framework code:
//
// ArrayList list = new ArrayList();
// list.Add(1);
// Array items = Array.CreateInstance(typeof(object), new int[] { 1 }, new int[] { -1 });
// list.CopyTo(items, 0);
if (srcLen > (destLen - (arrayIndex - destLB)))
throw new ArgumentException(SR.Argument_InsufficientSpaceToCopyCollection);
if (arrayIndex - destLB > destLen)
throw new ArgumentException(SR.Argument_IndexOutOfArrayBounds);
// We need to verify the index as we;
IBindableVector _this = JitHelpers.UnsafeCast<IBindableVector>(this);
for (uint i = 0; i < srcLen; i++)
{
array.SetValue(_this.GetAt(i), i + arrayIndex);
}
}
}
}
|