Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / windowsruntime / ireadonlydictionarytoimapviewadapter.cs
blob9a9c98ab4160bc06bb2323140aae6076a138672d
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 //
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>Microsoft</OWNER>
10 using System;
11 using System.Security;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Diagnostics.Contracts;
16 using System.Runtime.InteropServices;
17 using System.Runtime.CompilerServices;
19 namespace System.Runtime.InteropServices.WindowsRuntime
21 // This is a set of stub methods implementing the support for the IMapView`2 interface on managed
22 // objects that implement IReadOnlyDictionary`2. Used by the interop mashaling infrastructure.
24 // The methods on this class must be written VERY carefully to avoid introducing security holes.
25 // That's because they are invoked with special "this"! The "this" object
26 // for all of these methods are not IReadOnlyDictionaryToIMapViewAdapter objects. Rather, they are of type
27 // IReadOnlyDictionary<K, V>. No actual IReadOnlyDictionaryToIMapViewAdapter object is ever instantiated. Thus, you will
28 // see a lot of expressions that cast "this" to "IReadOnlyDictionary<K, V>".
29 [DebuggerDisplay("Size = {Size}")]
30 internal sealed class IReadOnlyDictionaryToIMapViewAdapter
32 private IReadOnlyDictionaryToIMapViewAdapter()
34 Contract.Assert(false, "This class is never instantiated");
37 // V Lookup(K key)
38 [SecurityCritical]
39 internal V Lookup<K, V>(K key)
41 IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
42 V value;
43 bool keyFound = _this.TryGetValue(key, out value);
45 if (!keyFound)
47 Exception e = new KeyNotFoundException(Environment.GetResourceString("Arg_KeyNotFound"));
48 e.SetErrorCode(__HResults.E_BOUNDS);
49 throw e;
52 return value;
55 // uint Size { get }
56 [SecurityCritical]
57 internal uint Size<K, V>()
59 IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
60 return (uint)_this.Count;
63 // bool HasKey(K key)
64 [SecurityCritical]
65 internal bool HasKey<K, V>(K key)
67 IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
68 return _this.ContainsKey(key);
71 // void Split(out IMapView<K, V> first, out IMapView<K, V> second)
72 [SecurityCritical]
73 internal void Split<K, V>(out IMapView<K, V> first, out IMapView<K, V> second)
75 IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
77 if (_this.Count < 2) {
78 first = null;
79 second = null;
80 return;
83 ConstantSplittableMap<K, V> splittableMap = _this as ConstantSplittableMap<K, V>;
85 if (splittableMap == null)
86 splittableMap = new ConstantSplittableMap<K, V>(_this);
88 splittableMap.Split(out first, out second);