3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>Microsoft</OWNER>
11 using System
.Security
;
12 using System
.Collections
;
13 using System
.Collections
.Generic
;
14 using System
.Diagnostics
.Contracts
;
15 using System
.Runtime
.InteropServices
;
16 using System
.Runtime
.CompilerServices
;
18 namespace System
.Runtime
.InteropServices
.WindowsRuntime
20 // This is a set of stub methods implementing the support for the ICollection`1 interface on WinRT
21 // objects that support IVector`1. Used by the interop mashaling infrastructure.
23 // The methods on this class must be written VERY carefully to avoid introducing security holes.
24 // That's because they are invoked with special "this"! The "this" object
25 // for all of these methods are not VectorToCollectionAdapter objects. Rather, they are of type
26 // IVector<T>. No actual VectorToCollectionAdapter object is ever instantiated. Thus, you will see
27 // a lot of expressions that cast "this" to "IVector<T>".
28 internal sealed class VectorToCollectionAdapter
30 private VectorToCollectionAdapter()
32 Contract
.Assert(false, "This class is never instantiated");
38 internal int Count
<T
>()
40 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
41 uint size
= _this
.Size
;
42 if (((uint)Int32
.MaxValue
) < size
)
44 throw new InvalidOperationException(Environment
.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
50 // bool IsReadOnly { get }
52 internal bool IsReadOnly
<T
>()
59 internal void Add
<T
>(T item
)
61 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
67 internal void Clear
<T
>()
69 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
73 // bool Contains(T item)
75 internal bool Contains
<T
>(T item
)
77 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
80 return _this
.IndexOf(item
, out index
);
83 // void CopyTo(T[] array, int arrayIndex)
85 internal void CopyTo
<T
>(T
[] array
, int arrayIndex
)
88 throw new ArgumentNullException("array");
91 throw new ArgumentOutOfRangeException("arrayIndex");
93 if (array
.Length
<= arrayIndex
&& Count
<T
>() > 0)
94 throw new ArgumentException(Environment
.GetResourceString("Argument_IndexOutOfArrayBounds"));
96 if (array
.Length
- arrayIndex
< Count
<T
>())
97 throw new ArgumentException(Environment
.GetResourceString("Argument_InsufficientSpaceToCopyCollection"));
99 Contract
.EndContractBlock();
101 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
102 int count
= Count
<T
>();
103 for (int i
= 0; i
< count
; i
++)
105 array
[i
+ arrayIndex
] = VectorToListAdapter
.GetAt
<T
>(_this
, (uint)i
);
109 // bool Remove(T item)
111 internal bool Remove
<T
>(T item
)
113 IVector
<T
> _this
= JitHelpers
.UnsafeCast
<IVector
<T
>>(this);
116 bool exists
= _this
.IndexOf(item
, out index
);
121 if (((uint)Int32
.MaxValue
) < index
)
123 throw new InvalidOperationException(Environment
.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
126 VectorToListAdapter
.RemoveAtHelper
<T
>(_this
, index
);