bc87efa6dcb22194a62a4fffe30429d22ff67bcc
[platform/upstream/dotnet/runtime.git] /
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.
4
5 //
6
7 using System;
8 using System.Security;
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Diagnostics;
12 using System.Runtime.InteropServices;
13 using System.Runtime.CompilerServices;
14 using Internal.Runtime.CompilerServices;
15
16 namespace System.Runtime.InteropServices.WindowsRuntime
17 {
18     // This is a set of stub methods implementing the support for the IReadOnlyCollection<T> interface on WinRT
19     // objects that support IVectorView<T>. Used by the interop mashaling infrastructure.
20     //
21     // The methods on this class must be written VERY carefully to avoid introducing security holes.
22     // That's because they are invoked with special "this"! The "this" object
23     // for all of these methods are not VectorViewToReadOnlyCollectionAdapter objects. Rather, they are of type
24     // IVectorView<T>. No actual VectorViewToReadOnlyCollectionAdapter object is ever instantiated. Thus, you will see
25     // a lot of expressions that cast "this" to "IVectorView<T>".
26     internal sealed class VectorViewToReadOnlyCollectionAdapter
27     {
28         private VectorViewToReadOnlyCollectionAdapter()
29         {
30             Debug.Fail("This class is never instantiated");
31         }
32
33         // int Count { get }
34         internal int Count<T>()
35         {
36             IVectorView<T> _this = Unsafe.As<IVectorView<T>>(this);
37             uint size = _this.Size;
38             if (((uint)int.MaxValue) < size)
39             {
40                 throw new InvalidOperationException(SR.InvalidOperation_CollectionBackingListTooLarge);
41             }
42
43             return (int)size;
44         }
45     }
46 }