-
Notifications
You must be signed in to change notification settings - Fork 107
/
ComWrappersSupport.cs
1091 lines (1020 loc) · 45.5 KB
/
ComWrappersSupport.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using ABI.Microsoft.UI.Xaml.Data;
using ABI.Windows.Foundation;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using WinRT.Interop;
#if NET
using ComInterfaceEntry = System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry;
#endif
#pragma warning disable 0169 // The field 'xxx' is never used
#pragma warning disable 0649 // Field 'xxx' is never assigned to, and will always have its default value
namespace WinRT
{
internal static class KeyValuePairHelper
{
internal readonly static ConcurrentDictionary<Type, ComInterfaceEntry> KeyValuePairCCW = new();
internal static void TryAddKeyValuePairCCW(Type keyValuePairType, Guid iid, IntPtr abiToProjectionVftablePtr)
{
KeyValuePairCCW.TryAdd(keyValuePairType, new ComInterfaceEntry { IID = iid, Vtable = abiToProjectionVftablePtr });
}
}
#if EMBED
internal
#else
public
#endif
static partial class ComWrappersSupport
{
private readonly static ConcurrentDictionary<Type, Func<IInspectable, object>> TypedObjectFactoryCacheForType = new();
private readonly static ConcurrentDictionary<Type, Func<IntPtr, object>> DelegateFactoryCache = new();
public static TReturn MarshalDelegateInvoke<TDelegate, TReturn>(IntPtr thisPtr, Func<TDelegate, TReturn> invoke)
where TDelegate : class, Delegate
{
#if !NET
using (new Mono.ThreadContext())
#endif
{
var target_invoke = FindObject<TDelegate>(thisPtr);
if (target_invoke != null)
{
return invoke(target_invoke);
}
return default;
}
}
public static void MarshalDelegateInvoke<T>(IntPtr thisPtr, Action<T> invoke)
where T : class, Delegate
{
#if !NET
using (new Mono.ThreadContext())
#endif
{
var target_invoke = FindObject<T>(thisPtr);
if (target_invoke != null)
{
invoke(target_invoke);
}
}
}
// If we are free threaded, we do not need to keep track of context.
// This can either be if the object implements IAgileObject or the free threaded marshaler.
internal unsafe static bool IsFreeThreaded(IObjectReference objRef)
{
if (objRef.TryAs(InterfaceIIDs.IAgileObject_IID, out var agilePtr) >= 0)
{
Marshal.Release(agilePtr);
return true;
}
else if (objRef.TryAs(InterfaceIIDs.IMarshal_IID, out var marshalPtr) >= 0)
{
try
{
Guid iid_IUnknown = InterfaceIIDs.IUnknown_IID;
Guid iid_unmarshalClass;
Marshal.ThrowExceptionForHR((**(ABI.WinRT.Interop.IMarshal.Vftbl**)marshalPtr).GetUnmarshalClass_0(
marshalPtr, &iid_IUnknown, IntPtr.Zero, MSHCTX.InProc, IntPtr.Zero, MSHLFLAGS.Normal, &iid_unmarshalClass));
if (iid_unmarshalClass == ABI.WinRT.Interop.IMarshal.IID_InProcFreeThreadedMarshaler.Value)
{
return true;
}
}
finally
{
Marshal.Release(marshalPtr);
}
}
return false;
}
public static IObjectReference GetObjectReferenceForInterface(IntPtr externalComObject)
{
return GetObjectReferenceForInterface<IUnknownVftbl>(externalComObject);
}
public static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr externalComObject)
{
if (externalComObject == IntPtr.Zero)
{
return null;
}
ObjectReference<T> objRef = ObjectReference<T>.FromAbi(externalComObject);
if (IsFreeThreaded(objRef))
{
return objRef;
}
else
{
using (objRef)
{
return new ObjectReferenceWithContext<T>(
objRef.GetRef(),
Context.GetContextCallback(),
Context.GetContextToken());
}
}
}
public static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr externalComObject, Guid iid)
{
return GetObjectReferenceForInterface<T>(externalComObject, iid, true);
}
internal static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr externalComObject, Guid iid, bool requireQI)
{
if (externalComObject == IntPtr.Zero)
{
return null;
}
ObjectReference<T> objRef;
if (requireQI)
{
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(externalComObject, ref iid, out IntPtr ptr));
objRef = ObjectReference<T>.Attach(ref ptr);
}
else
{
objRef = ObjectReference<T>.FromAbi(externalComObject);
}
if (IsFreeThreaded(objRef))
{
return objRef;
}
else
{
using (objRef)
{
return new ObjectReferenceWithContext<T>(
objRef.GetRef(),
Context.GetContextCallback(),
Context.GetContextToken(),
iid);
}
}
}
public static void RegisterProjectionAssembly(Assembly assembly) => TypeNameSupport.RegisterProjectionAssembly(assembly);
public static void RegisterProjectionTypeBaseTypeMapping(IDictionary<string, string> typeNameToBaseTypeNameMapping) => TypeNameSupport.RegisterProjectionTypeBaseTypeMapping(typeNameToBaseTypeNameMapping);
public static void RegisterAuthoringMetadataTypeLookup(Func<Type, Type> authoringMetadataTypeLookup) => TypeExtensions.RegisterAuthoringMetadataTypeLookup(authoringMetadataTypeLookup);
internal static List<ComInterfaceEntry> GetInterfaceTableEntries(Type type)
{
var entries = new List<ComInterfaceEntry>();
bool hasCustomIMarshalInterface = false;
bool hasWinrtExposedClassAttribute = false;
#if NET
// Check whether the type itself has the WinRTTypeExposed attribute and if so
// use the new source generator approach.
var winrtExposedClassAttribute = type.GetCustomAttribute<WinRTExposedTypeAttribute>(false);
// Handle scenario where it can be an authored type
// which means the attribute lives on the authoring metadata type.
if (winrtExposedClassAttribute == null)
{
var authoringMetadaType = type.GetRuntimeClassCCWType();
if (authoringMetadaType != null)
{
winrtExposedClassAttribute = authoringMetadaType.GetCustomAttribute<WinRTExposedTypeAttribute>(false);
}
}
if (winrtExposedClassAttribute != null)
{
hasWinrtExposedClassAttribute = true;
entries.AddRange(winrtExposedClassAttribute.GetExposedInterfaces());
if (type.IsClass)
{
hasCustomIMarshalInterface = entries.Any(entry => entry.IID == ABI.WinRT.Interop.IMarshal.IID);
}
}
else if (type == typeof(global::System.EventHandler))
{
hasWinrtExposedClassAttribute = true;
entries.AddRange(ABI.System.EventHandler.GetExposedInterfaces());
}
else if (ComInterfaceEntriesForType.TryGetValue(type, out var registeredEntries))
{
hasWinrtExposedClassAttribute = true;
entries.AddRange(registeredEntries);
}
else if (!type.IsEnum && GetComInterfaceEntriesForTypeFromLookupTable(type) is var lookupTableEntries && lookupTableEntries != null)
{
hasWinrtExposedClassAttribute = true;
entries.AddRange(lookupTableEntries);
}
else if (RuntimeFeature.IsDynamicCodeCompiled)
#endif
{
if (type.IsDelegate())
{
// Delegates have no interfaces that they implement, so adding default WinRT entries.
var helperType = type.FindHelperType();
if (helperType is object)
{
entries.Add(new ComInterfaceEntry
{
IID = GuidGenerator.GetIID(type),
Vtable = helperType.GetAbiToProjectionVftblPtr()
});
}
if (type.ShouldProvideIReference())
{
entries.Add(IPropertyValueEntry);
entries.Add(ProvideIReference(type));
}
}
else
{
var objType = type.GetRuntimeClassCCWType() ?? type;
var interfaces = objType.GetInterfaces();
foreach (var iface in interfaces)
{
if (Projections.IsTypeWindowsRuntimeType(iface))
{
AddInterfaceToVtable(iface);
}
if (iface.IsConstructedGenericType
&& Projections.TryGetCompatibleWindowsRuntimeTypesForVariantType(iface, null, out var compatibleIfaces))
{
foreach (var compatibleIface in compatibleIfaces)
{
AddInterfaceToVtable(compatibleIface);
}
}
}
}
}
#if !NET
// We can't easily determine from just the type
// if the array is an "single dimension index from zero"-array in .NET Standard 2.0,
// so just approximate it.
// (Other array types will be blocked in other code-paths anyway where we have an object.)
if (type.IsArray && type.GetArrayRank() == 1)
#else
if (type.IsSZArray)
#endif
{
// We treat arrays as if they implemented IIterable<T>, IVector<T>, and IVectorView<T> (WinRT only)
var elementType = type.GetElementType();
if (elementType.ShouldProvideIReference())
{
entries.Add(IPropertyValueEntry);
entries.Add(ProvideIReferenceArray(type));
}
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Collections.Generic.KeyValuePair<,>))
{
if (KeyValuePairHelper.KeyValuePairCCW.TryGetValue(type, out var entry))
{
entries.Add(entry);
}
else
{
var ifaceAbiType = type.FindHelperType();
entries.Add(new ComInterfaceEntry
{
IID = GuidGenerator.GetIID(ifaceAbiType),
Vtable = (IntPtr)ifaceAbiType.GetAbiToProjectionVftblPtr()
});
}
}
else if (!hasWinrtExposedClassAttribute && type.ShouldProvideIReference())
{
entries.Add(IPropertyValueEntry);
entries.Add(ProvideIReference(type));
}
entries.Add(new ComInterfaceEntry
{
IID = ManagedIStringableVftbl.IID,
Vtable = ManagedIStringableVftbl.AbiToProjectionVftablePtr
});
entries.Add(new ComInterfaceEntry
{
IID = ManagedCustomPropertyProviderVftbl.IID,
Vtable = ManagedCustomPropertyProviderVftbl.AbiToProjectionVftablePtr
});
entries.Add(new ComInterfaceEntry
{
IID = InterfaceIIDs.IWeakReferenceSource_IID,
Vtable = ABI.WinRT.Interop.IWeakReferenceSource.AbiToProjectionVftablePtr
});
// Add IMarhal implemented using the free threaded marshaler
// to all CCWs if it doesn't already have its own.
if (!hasCustomIMarshalInterface)
{
entries.Add(new ComInterfaceEntry
{
IID = InterfaceIIDs.IMarshal_IID,
Vtable = ABI.WinRT.Interop.IMarshal.Vftbl.AbiToProjectionVftablePtr
});
}
// Add IAgileObject to all CCWs
entries.Add(new ComInterfaceEntry
{
IID = InterfaceIIDs.IAgileObject_IID,
Vtable = IUnknownVftbl.AbiToProjectionVftblPtr
});
entries.Add(new ComInterfaceEntry
{
IID = InterfaceIIDs.IInspectable_IID,
Vtable = IInspectable.Vftbl.AbiToProjectionVftablePtr
});
// This should be the last entry as it is included / excluded based on the flags.
entries.Add(new ComInterfaceEntry
{
IID = InterfaceIIDs.IUnknown_IID,
Vtable = IUnknownVftbl.AbiToProjectionVftblPtr
});
return entries;
void AddInterfaceToVtable(Type iface)
{
var interfaceHelperType = iface.FindHelperType();
Guid iid = GuidGenerator.GetIID(interfaceHelperType);
entries.Add(new ComInterfaceEntry
{
IID = GuidGenerator.GetIID(interfaceHelperType),
Vtable = (IntPtr)interfaceHelperType.GetAbiToProjectionVftblPtr()
});
if (!hasCustomIMarshalInterface && iid == InterfaceIIDs.IMarshal_IID)
{
hasCustomIMarshalInterface = true;
}
}
}
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "The existence of the ABI type implies the non-ABI type exists, as in authoring scenarios the ABI type is constructed from the non-ABI type.")]
#endif
internal static (InspectableInfo inspectableInfo, List<ComInterfaceEntry> interfaceTableEntries) PregenerateNativeTypeInformation(Type type)
{
var interfaceTableEntries = GetInterfaceTableEntries(type);
var iids = new Guid[interfaceTableEntries.Count];
for (int i = 0; i < interfaceTableEntries.Count; i++)
{
iids[i] = interfaceTableEntries[i].IID;
}
if (type.FullName.StartsWith("ABI.", StringComparison.Ordinal))
{
type = Projections.FindCustomPublicTypeForAbiType(type) ?? type.Assembly.GetType(type.FullName.Substring("ABI.".Length)) ?? type;
}
return (
new InspectableInfo(type, iids),
interfaceTableEntries);
}
private static Func<IInspectable, object> CreateKeyValuePairFactory(Type type)
{
var createRcwFunc = (Func<IInspectable, object>) type.GetHelperType().GetMethod("CreateRcw", BindingFlags.Public | BindingFlags.Static).
CreateDelegate(typeof(Func<IInspectable, object>));
return createRcwFunc;
}
internal static Func<IntPtr, object> CreateDelegateFactory(Type type)
{
return DelegateFactoryCache.GetOrAdd(type, (type) =>
{
var createRcwFunc = (Func<IntPtr, object>)type.GetHelperType().GetMethod("CreateRcw", BindingFlags.Public | BindingFlags.Static).
CreateDelegate(typeof(Func<IntPtr, object>));
var iid = GuidGenerator.GetIID(type);
return (IntPtr externalComObject) =>
{
// The CreateRCW function for delegates expect the pointer to be the delegate interface in CsWinRT 1.5.
// But CreateObject is passed the IUnknown interface. This would typically be fine for delegates as delegates
// don't implement interfaces and implementations typically have both the IUnknown vtable and the delegate
// vtable point to the same vtable. But when the pointer is to a proxy, that can not be relied on.
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(externalComObject, ref iid, out var ptr));
try
{
return createRcwFunc(ptr);
}
finally
{
Marshal.Release(ptr);
}
};
});
}
public static bool RegisterDelegateFactory(Type implementationType, Func<IntPtr, object> delegateFactory) => DelegateFactoryCache.TryAdd(implementationType, delegateFactory);
private static Func<IInspectable, object> CreateNullableTFactory(Type implementationType)
{
var getValueMethod = implementationType.GetHelperType().GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic);
return (IInspectable obj) => getValueMethod.Invoke(null, new[] { obj });
}
private static Func<IInspectable, object> CreateAbiNullableTFactory(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type implementationType)
{
var getValueMethod = implementationType.GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic);
return (IInspectable obj) => getValueMethod.Invoke(null, new[] { obj });
}
private static Func<IInspectable, object> CreateArrayFactory(Type implementationType)
{
var getValueFunc = (Func<IInspectable, object>)implementationType.GetHelperType().GetMethod("GetValue", BindingFlags.Static | BindingFlags.NonPublic).
CreateDelegate(typeof(Func<IInspectable, object>));
return getValueFunc;
}
// This is used to hold the reference to the native value type object (IReference) until the actual value in it (boxed as an object) gets cleaned up by GC
// This is done to avoid pointer reuse until GC cleans up the boxed object
private static readonly ConditionalWeakTable<object, IInspectable> _boxedValueReferenceCache = new();
private static Func<IInspectable, object> CreateReferenceCachingFactory(Func<IInspectable, object> internalFactory)
{
return inspectable =>
{
object resultingObject = internalFactory(inspectable);
_boxedValueReferenceCache.Add(resultingObject, inspectable);
return resultingObject;
};
}
private static Func<IInspectable, object> CreateCustomTypeMappingFactory(Type customTypeHelperType)
{
var fromAbiMethod = customTypeHelperType.GetMethod("FromAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (fromAbiMethod is null)
{
throw new MissingMethodException();
}
var fromAbiMethodFunc = (Func<IntPtr, object>) fromAbiMethod.CreateDelegate(typeof(Func<IntPtr, object>));
return (IInspectable obj) => fromAbiMethodFunc(obj.ThisPtr);
}
internal static Func<IInspectable, object> CreateTypedRcwFactory(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
Type implementationType,
string runtimeClassName = null)
{
// If runtime class name is empty or "Object", then just use IInspectable.
if (implementationType == null || implementationType == typeof(object))
{
// If we reach here, then we couldn't find a type that matches the runtime class name.
// Fall back to using IInspectable directly.
return (IInspectable obj) => obj;
}
if (implementationType == typeof(ABI.System.Nullable_string))
{
return CreateReferenceCachingFactory(ABI.System.Nullable_string.GetValue);
}
else if (implementationType == typeof(ABI.System.Nullable_Type))
{
return CreateReferenceCachingFactory(ABI.System.Nullable_Type.GetValue);
}
else if (implementationType == typeof(ABI.System.Nullable_Exception))
{
return CreateReferenceCachingFactory(ABI.System.Nullable_Exception.GetValue);
}
var customHelperType = Projections.FindCustomHelperTypeMapping(implementationType, true);
if (customHelperType != null)
{
return CreateReferenceCachingFactory(CreateCustomTypeMappingFactory(customHelperType));
}
if (implementationType.IsGenericType && implementationType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.KeyValuePair<,>))
{
return CreateReferenceCachingFactory(CreateKeyValuePairFactory(implementationType));
}
if (implementationType.IsValueType)
{
if (implementationType.IsNullableT())
{
return CreateReferenceCachingFactory(CreateNullableTFactory(implementationType));
}
else
{
return CreateReferenceCachingFactory(CreateNullableTFactory(typeof(System.Nullable<>).MakeGenericType(implementationType)));
}
}
else if (implementationType.IsAbiNullableDelegate())
{
return CreateReferenceCachingFactory(CreateAbiNullableTFactory(implementationType));
}
else if (implementationType.IsIReferenceArray())
{
return CreateReferenceCachingFactory(CreateArrayFactory(implementationType));
}
return CreateFactoryForImplementationType(runtimeClassName, implementationType);
}
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
internal static Type GetRuntimeClassForTypeCreation(IInspectable inspectable, Type staticallyDeterminedType)
{
string runtimeClassName = inspectable.GetRuntimeClassName(noThrow: true);
Type implementationType = null;
if (!string.IsNullOrEmpty(runtimeClassName))
{
implementationType = TypeNameSupport.FindRcwTypeByNameCached(runtimeClassName);
}
if (staticallyDeterminedType != null && staticallyDeterminedType != typeof(object))
{
// We have a static type which we can use to construct the object. But, we can't just use it for all scenarios
// and primarily use it for tear off scenarios and for scenarios where runtimeclass isn't accurate.
// For instance if the static type is an interface, we return an IInspectable to represent the interface.
// But it isn't convertable back to the class via the as operator which would be possible if we use runtimeclass.
// Similarly for composable types, they can be statically retrieved using the parent class, but can then no longer
// be cast to the sub class via as operator even if it is really an instance of it per rutimeclass.
// To handle these scenarios, we use the runtimeclass if we find it is assignable to the statically determined type.
// If it isn't, we use the statically determined type as it is a tear off.
if (!(implementationType != null &&
(staticallyDeterminedType == implementationType ||
staticallyDeterminedType.IsAssignableFrom(implementationType) ||
staticallyDeterminedType.IsGenericType && implementationType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == staticallyDeterminedType.GetGenericTypeDefinition()))))
{
return staticallyDeterminedType;
}
}
return implementationType;
}
private static ComInterfaceEntry IPropertyValueEntry =>
new ComInterfaceEntry
{
IID = ManagedIPropertyValueImpl.IID,
Vtable = ManagedIPropertyValueImpl.AbiToProjectionVftablePtr
};
private static ComInterfaceEntry ProvideIReference(Type type)
{
if (type == typeof(int))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_int.IID,
Vtable = ABI.System.Nullable_int.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(string))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_string.IID,
Vtable = ABI.System.Nullable_string.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(byte))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_byte.IID,
Vtable = ABI.System.Nullable_byte.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(short))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_short.IID,
Vtable = ABI.System.Nullable_short.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(ushort))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_ushort.IID,
Vtable = ABI.System.Nullable_ushort.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(uint))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_uint.IID,
Vtable = ABI.System.Nullable_uint.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(long))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_long.IID,
Vtable = ABI.System.Nullable_long.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(ulong))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_ulong.IID,
Vtable = ABI.System.Nullable_ulong.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(float))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_float.IID,
Vtable = ABI.System.Nullable_float.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(double))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_double.IID,
Vtable = ABI.System.Nullable_double.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(char))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_char.IID,
Vtable = ABI.System.Nullable_char.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(bool))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_bool.IID,
Vtable = ABI.System.Nullable_bool.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(Guid))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_guid.IID,
Vtable = ABI.System.Nullable_guid.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(DateTimeOffset))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_DateTimeOffset.IID,
Vtable = ABI.System.Nullable_DateTimeOffset.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(TimeSpan))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_TimeSpan.IID,
Vtable = ABI.System.Nullable_TimeSpan.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(object))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_Object.IID,
Vtable = ABI.System.Nullable_Object.Vftbl.AbiToProjectionVftablePtr
};
}
if (type.IsTypeOfType())
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_Type.IID,
Vtable = ABI.System.Nullable_Type.Vftbl.AbiToProjectionVftablePtr
};
}
if (type == typeof(sbyte))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_sbyte.IID,
Vtable = ABI.System.Nullable_sbyte.Vftbl.AbiToProjectionVftablePtr
};
}
if (type.IsEnum)
{
if (type.IsDefined(typeof(FlagsAttribute)))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_FlagsEnum.GetIID(type),
Vtable = ABI.System.Nullable_FlagsEnum.AbiToProjectionVftablePtr
};
}
else
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_IntEnum.GetIID(type),
Vtable = ABI.System.Nullable_IntEnum.AbiToProjectionVftablePtr
};
}
}
if (type == typeof(EventHandler))
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_EventHandler.IID,
Vtable = ABI.System.Nullable_EventHandler.AbiToProjectionVftablePtr
};
}
if (type.IsDelegate())
{
var delegateHelperType = typeof(ABI.System.Nullable_Delegate<>).MakeGenericType(type);
return new ComInterfaceEntry
{
IID = global::WinRT.GuidGenerator.GetIID(delegateHelperType),
Vtable = delegateHelperType.GetAbiToProjectionVftblPtr()
};
}
if (type == typeof(System.Numerics.Matrix3x2))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceMatrix3x2_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Matrix3x2, System.Numerics.Matrix3x2>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Matrix4x4))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceMatrix4x4_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Matrix4x4, System.Numerics.Matrix4x4>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Plane))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferencePlane_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Plane, System.Numerics.Plane>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Quaternion))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceQuaternion_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Quaternion, System.Numerics.Quaternion>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Vector2))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceVector2_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Vector2, System.Numerics.Vector2>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Vector3))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceVector3_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Vector3, System.Numerics.Vector3>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Vector4))
{
return new ComInterfaceEntry
{
IID = ABI.System.IReferenceIIDs.IReferenceVector4_IID,
Vtable = BoxedValueIReferenceImpl<System.Numerics.Vector4, System.Numerics.Vector4>.AbiToProjectionVftablePtr
};
}
if (type.IsTypeOfException())
{
return new ComInterfaceEntry
{
IID = ABI.System.Nullable_Exception.IID,
Vtable = ABI.System.Nullable_Exception.Vftbl.AbiToProjectionVftablePtr
};
}
return new ComInterfaceEntry
{
IID = global::WinRT.GuidGenerator.GetIID(typeof(ABI.System.Nullable<>).MakeGenericType(type)),
Vtable = typeof(BoxedValueIReferenceImpl<>).MakeGenericType(type).GetAbiToProjectionVftblPtr()
};
}
private static ComInterfaceEntry ProvideIReferenceArray(Type arrayType)
{
Type type = arrayType.GetElementType();
if (type == typeof(int))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfInt32_IID,
Vtable = BoxedArrayIReferenceArrayImpl<int>.AbiToProjectionVftablePtr
};
}
if (type == typeof(string))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfString_IID,
Vtable = BoxedArrayIReferenceArrayImpl<string>.AbiToProjectionVftablePtr
};
}
if (type == typeof(byte))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfByte_IID,
Vtable = BoxedArrayIReferenceArrayImpl<byte>.AbiToProjectionVftablePtr
};
}
if (type == typeof(short))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfInt16_IID,
Vtable = BoxedArrayIReferenceArrayImpl<short>.AbiToProjectionVftablePtr
};
}
if (type == typeof(ushort))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfUInt16_IID,
Vtable = BoxedArrayIReferenceArrayImpl<ushort>.AbiToProjectionVftablePtr
};
}
if (type == typeof(uint))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfUInt32_IID,
Vtable = BoxedArrayIReferenceArrayImpl<uint>.AbiToProjectionVftablePtr
};
}
if (type == typeof(long))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfInt64_IID,
Vtable = BoxedArrayIReferenceArrayImpl<long>.AbiToProjectionVftablePtr
};
}
if (type == typeof(ulong))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfUInt64_IID,
Vtable = BoxedArrayIReferenceArrayImpl<ulong>.AbiToProjectionVftablePtr
};
}
if (type == typeof(float))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfSingle_IID,
Vtable = BoxedArrayIReferenceArrayImpl<float>.AbiToProjectionVftablePtr
};
}
if (type == typeof(double))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfDouble_IID,
Vtable = BoxedArrayIReferenceArrayImpl<double>.AbiToProjectionVftablePtr
};
}
if (type == typeof(char))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfChar_IID,
Vtable = BoxedArrayIReferenceArrayImpl<char>.AbiToProjectionVftablePtr
};
}
if (type == typeof(bool))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfBoolean_IID,
Vtable = BoxedArrayIReferenceArrayImpl<bool>.AbiToProjectionVftablePtr
};
}
if (type == typeof(Guid))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfGuid_IID,
Vtable = BoxedArrayIReferenceArrayImpl<Guid>.AbiToProjectionVftablePtr
};
}
if (type == typeof(DateTimeOffset))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfDateTimeOffset_IID,
Vtable = BoxedArrayIReferenceArrayImpl<DateTimeOffset>.AbiToProjectionVftablePtr
};
}
if (type == typeof(TimeSpan))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfTimeSpan_IID,
Vtable = BoxedArrayIReferenceArrayImpl<TimeSpan>.AbiToProjectionVftablePtr
};
}
if (type == typeof(object))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfObject_IID,
Vtable = BoxedArrayIReferenceArrayImpl<object>.AbiToProjectionVftablePtr
};
}
if (type.IsTypeOfType())
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfType_IID,
Vtable = BoxedArrayIReferenceArrayImpl<Type>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Matrix3x2))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfMatrix3x2_IID,
Vtable = BoxedArrayIReferenceArrayImpl<System.Numerics.Matrix3x2>.AbiToProjectionVftablePtr
};
}
if (type == typeof(System.Numerics.Matrix4x4))
{
return new ComInterfaceEntry
{
IID = IReferenceArrayIIDs.IReferenceArrayOfMatrix4x4_IID,
Vtable = BoxedArrayIReferenceArrayImpl<System.Numerics.Matrix4x4>.AbiToProjectionVftablePtr
};
}