forked from microsoft/CsWinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIWinRTObject.net5.cs
More file actions
194 lines (176 loc) · 7.92 KB
/
IWinRTObject.net5.cs
File metadata and controls
194 lines (176 loc) · 7.92 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
interface IWinRTObject : IDynamicInterfaceCastable
{
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
return IsInterfaceImplementedFallback(interfaceType, throwIfNotImplemented);
}
bool IsInterfaceImplementedFallback(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
if (QueryInterfaceCache.ContainsKey(interfaceType))
{
return true;
}
Type type = Type.GetTypeFromHandle(interfaceType);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IReadOnlyCollection<>))
{
Type itemType = type.GetGenericArguments()[0];
if (itemType.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
Type iReadOnlyDictionary = typeof(IReadOnlyDictionary<,>).MakeGenericType(itemType.GetGenericArguments());
if (IsInterfaceImplemented(iReadOnlyDictionary.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iReadOnlyDictionary.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
Type iReadOnlyList = typeof(IReadOnlyList<>).MakeGenericType(new[] { itemType });
if (IsInterfaceImplemented(iReadOnlyList.TypeHandle, throwIfNotImplemented))
{
if (QueryInterfaceCache.TryGetValue(iReadOnlyList.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
return false;
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.ICollection<>))
{
Type itemType = type.GetGenericArguments()[0];
if (itemType.IsGenericType && itemType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
Type iDictionary = typeof(IDictionary<,>).MakeGenericType(itemType.GetGenericArguments());
if (IsInterfaceImplemented(iDictionary.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iDictionary.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
Type iList = typeof(IList<>).MakeGenericType(new[] { itemType });
if (IsInterfaceImplemented(iList.TypeHandle, throwIfNotImplemented))
{
if (QueryInterfaceCache.TryGetValue(iList.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
return false;
}
else if (type == typeof(System.Collections.IEnumerable))
{
Type iEnum = typeof(System.Collections.Generic.IEnumerable<object>);
if (IsInterfaceImplemented(iEnum.TypeHandle, false))
{
if (QueryInterfaceCache.TryGetValue(iEnum.TypeHandle, out var typedObjRef) && !QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
Type helperType = type.FindHelperType();
if (helperType is null || !helperType.IsInterface)
{
return false;
}
int hr = NativeObject.TryAs<IUnknownVftbl>(GuidGenerator.GetIID(helperType), out var objRef);
if (hr < 0)
{
if (throwIfNotImplemented)
{
ExceptionHelpers.ThrowExceptionForHR(hr);
}
return false;
}
if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
{
RuntimeTypeHandle projectIEnum = typeof(System.Collections.IEnumerable).TypeHandle;
AdditionalTypeData.GetOrAdd(projectIEnum, (_) => new ABI.System.Collections.IEnumerable.AdaptiveFromAbiHelper(type, this));
}
var vftblType = helperType.FindVftblType();
using (objRef)
{
if (vftblType is null)
{
var qiObjRef = objRef.As<IUnknownVftbl>(GuidGenerator.GetIID(helperType));
if (!QueryInterfaceCache.TryAdd(interfaceType, qiObjRef))
{
qiObjRef.Dispose();
}
return true;
}
IObjectReference typedObjRef = (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null);
if (!QueryInterfaceCache.TryAdd(interfaceType, typedObjRef))
{
typedObjRef.Dispose();
}
return true;
}
}
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
{
var type = Type.GetTypeFromHandle(interfaceType);
var helperType = type.GetHelperType();
if (helperType.IsInterface)
return helperType.TypeHandle;
return default;
}
IObjectReference NativeObject { get; }
bool HasUnwrappableNativeObject { get; }
protected ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> QueryInterfaceCache { get; }
IObjectReference GetObjectReferenceForType(RuntimeTypeHandle type)
{
return GetObjectReferenceForTypeFallback(type);
}
IObjectReference GetObjectReferenceForTypeFallback(RuntimeTypeHandle type)
{
if (IsInterfaceImplemented(type, true))
{
return QueryInterfaceCache[type];
}
throw new Exception("Interface " + Type.GetTypeFromHandle(type) +" is not implemented.");
}
ConcurrentDictionary<RuntimeTypeHandle, object> AdditionalTypeData { get; }
object GetOrCreateTypeHelperData(RuntimeTypeHandle type, Func<object> helperDataFactory)
{
return AdditionalTypeData.GetOrAdd(type, (type) => helperDataFactory());
}
internal void Resurrect()
{
if (NativeObject.Resurrect())
{
foreach (var cached in QueryInterfaceCache)
{
cached.Value.Resurrect();
}
// Delegates store their agile reference as an additional type data.
// These should be recreated when instances are resurrect.
if (AdditionalTypeData.TryGetValue(typeof(AgileReference).TypeHandle, out var agileObj))
{
AdditionalTypeData.TryUpdate(typeof(AgileReference).TypeHandle, new AgileReference(NativeObject), agileObj);
}
}
}
}
}