summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/IVectorViewToIReadOnlyListAdapter.cs
blob: 37f21307dcc7b60716c88709c35b486985029c3f (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
// 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.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
{
    internal delegate T Indexer_Get_Delegate<out T>(int index);

    // This is a set of stub methods implementing the support for the IReadOnlyList`1 interface on WinRT
    // objects that support IVectorView`1. 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 IVectorViewToIReadOnlyListAdapter objects. Rather, they are of type
    // IVectorView<T>. No actual IVectorViewToIReadOnlyListAdapter object is ever instantiated. Thus, you will see
    // a lot of expressions that cast "this" to "IVectorView<T>".
    [DebuggerDisplay("Count = {Count}")]
    internal sealed class IVectorViewToIReadOnlyListAdapter
    {
        private IVectorViewToIReadOnlyListAdapter()
        {
            Debug.Assert(false, "This class is never instantiated");
        }

        // T this[int index] { get }
        internal T Indexer_Get<T>(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            IVectorView<T> _this = JitHelpers.UnsafeCast<IVectorView<T>>(this);

            try
            {
                return _this.GetAt((uint) index);

                // We delegate bounds checking to the underlying collection and if it detected a fault,
                // we translate it to the right exception:
            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new ArgumentOutOfRangeException(nameof(index));

                throw;
            }
        }

        // T this[int index] { get }
        internal T Indexer_Get_Variance<T>(int index) where T : class
        {
            bool fUseString;
            Delegate target = System.StubHelpers.StubHelpers.GetTargetForAmbiguousVariantCall(
                this, 
                typeof(IReadOnlyList<T>).TypeHandle.Value, 
                out fUseString);

            if (target != null)
            {
                return (JitHelpers.UnsafeCast<Indexer_Get_Delegate<T>>(target))(index);
            }
            
            if (fUseString)
            {
                return JitHelpers.UnsafeCast<T>(Indexer_Get<string>(index));
            }

            return Indexer_Get<T>(index);
        }
    }
}