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
;
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");
39 internal V Lookup
<K
, V
>(K key
)
41 IReadOnlyDictionary
<K
, V
> _this
= JitHelpers
.UnsafeCast
<IReadOnlyDictionary
<K
, V
>>(this);
43 bool keyFound
= _this
.TryGetValue(key
, out value);
47 Exception e
= new KeyNotFoundException(Environment
.GetResourceString("Arg_KeyNotFound"));
48 e
.SetErrorCode(__HResults
.E_BOUNDS
);
57 internal uint Size
<K
, V
>()
59 IReadOnlyDictionary
<K
, V
> _this
= JitHelpers
.UnsafeCast
<IReadOnlyDictionary
<K
, V
>>(this);
60 return (uint)_this
.Count
;
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)
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) {
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
);