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