-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathSingleInterfaceOptimizedObject.net5.cs
More file actions
117 lines (96 loc) · 4.32 KB
/
SingleInterfaceOptimizedObject.net5.cs
File metadata and controls
117 lines (96 loc) · 4.32 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
sealed class SingleInterfaceOptimizedObject : IWinRTObject, IDynamicInterfaceCastable
{
private readonly Type _type;
private readonly IObjectReference _obj;
public SingleInterfaceOptimizedObject(Type type, IObjectReference objRef)
: this(type, objRef, true)
{
}
internal SingleInterfaceOptimizedObject(Type type, IObjectReference objRef, bool requireQI)
{
_type = type;
if (requireQI)
{
Type helperType = type.FindHelperType();
if (RuntimeFeature.IsDynamicCodeCompiled)
{
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "If the 'Vftbl' type is kept, we can assume all its metadata will also have been rooted.")]
[MethodImpl(MethodImplOptions.NoInlining)]
static IObjectReference TryGetObjectReferenceViaVftbl(IObjectReference objRef, Type helperType)
{
var vftblType = helperType.FindVftblType();
if (vftblType is not null)
{
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
return (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null);
#pragma warning restore IL3050
}
return null;
}
IObjectReference objRefViaVftbl = TryGetObjectReferenceViaVftbl(objRef, helperType);
if (objRefViaVftbl is not null)
{
_obj = objRefViaVftbl;
return;
}
}
_obj = objRef.As<IUnknownVftbl>(GuidGenerator.GetIID(helperType));
}
else
{
_obj = objRef;
}
}
IObjectReference IWinRTObject.NativeObject => _obj;
bool IWinRTObject.HasUnwrappableNativeObject => false;
private volatile ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> _queryInterfaceCache;
private ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> MakeQueryInterfaceCache()
{
System.Threading.Interlocked.CompareExchange(ref _queryInterfaceCache, new ConcurrentDictionary<RuntimeTypeHandle, IObjectReference>(), null);
return _queryInterfaceCache;
}
ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> IWinRTObject.QueryInterfaceCache => _queryInterfaceCache ?? MakeQueryInterfaceCache();
private volatile ConcurrentDictionary<RuntimeTypeHandle, object> _additionalTypeData;
private ConcurrentDictionary<RuntimeTypeHandle, object> MakeAdditionalTypeData()
{
System.Threading.Interlocked.CompareExchange(ref _additionalTypeData, new ConcurrentDictionary<RuntimeTypeHandle, object>(), null);
return _additionalTypeData;
}
ConcurrentDictionary<RuntimeTypeHandle, object> IWinRTObject.AdditionalTypeData => _additionalTypeData ?? MakeAdditionalTypeData();
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
if (_type.Equals(Type.GetTypeFromHandle(interfaceType)))
{
return true;
}
if (!FeatureSwitches.EnableIDynamicInterfaceCastableSupport)
{
return false;
}
return ((IWinRTObject)this).IsInterfaceImplementedFallback(interfaceType, throwIfNotImplemented);
}
IObjectReference IWinRTObject.GetObjectReferenceForType(RuntimeTypeHandle interfaceType)
{
if (_type.Equals(Type.GetTypeFromHandle(interfaceType)))
{
return _obj;
}
return ((IWinRTObject)this).GetObjectReferenceForTypeFallback(interfaceType);
}
}
}