-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathProjections.cs
709 lines (640 loc) · 34.8 KB
/
Projections.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Input;
using Windows.Foundation.Collections;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
static partial class Projections
{
private static readonly ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim();
private static readonly Dictionary<Type, Type> CustomTypeToHelperTypeMappings = new Dictionary<Type, Type>();
private static readonly Dictionary<Type, Type> CustomAbiTypeToTypeMappings = new Dictionary<Type, Type>();
private static readonly Dictionary<string, Type> CustomAbiTypeNameToTypeMappings = new Dictionary<string, Type>(StringComparer.Ordinal);
private static readonly Dictionary<Type, string> CustomTypeToAbiTypeNameMappings = new Dictionary<Type, string>();
private static readonly HashSet<string> ProjectedRuntimeClassNames = new HashSet<string>(StringComparer.Ordinal);
private static readonly HashSet<Type> ProjectedCustomTypeRuntimeClasses = new HashSet<Type>();
static Projections()
{
// We always register mappings for 'bool' and 'char' as they're primitive types.
// They're also very cheap anyway and commonly used, so this keeps things simpler.
RegisterCustomAbiTypeMappingNoLock(typeof(bool), typeof(ABI.System.Boolean), "Boolean");
RegisterCustomAbiTypeMappingNoLock(typeof(char), typeof(ABI.System.Char), "Char");
// Also always register Type, since it's "free" (no associated ABI type to root)
CustomTypeToAbiTypeNameMappings.Add(typeof(Type), "Windows.UI.Xaml.Interop.TypeName");
#if NET
// If default mappings are disabled, we avoid rooting everything by default.
// Developers will have to optionally opt-in into individual mappings later.
// Only do this on modern .NET, because trimming isn't supported downlevel
// anyway. This also makes it simpler to expose all 'Register' methods.
if (!FeatureSwitches.EnableDefaultCustomTypeMappings)
{
return;
}
#endif
// This should be in sync with cswinrt/helpers.h and the reverse mapping from WinRT.SourceGenerator/WinRTTypeWriter.cs.
RegisterCustomAbiTypeMappingNoLock(typeof(EventRegistrationToken), typeof(ABI.WinRT.EventRegistrationToken), "Windows.Foundation.EventRegistrationToken");
RegisterCustomAbiTypeMappingNoLock(typeof(Nullable<>), typeof(ABI.System.Nullable<>), "Windows.Foundation.IReference`1");
RegisterCustomAbiTypeMappingNoLock(typeof(DateTimeOffset), typeof(ABI.System.DateTimeOffset), "Windows.Foundation.DateTime");
RegisterCustomAbiTypeMappingNoLock(typeof(Exception), typeof(ABI.System.Exception), "Windows.Foundation.HResult");
RegisterCustomAbiTypeMappingNoLock(typeof(TimeSpan), typeof(ABI.System.TimeSpan), "Windows.Foundation.TimeSpan");
RegisterCustomAbiTypeMappingNoLock(typeof(Uri), typeof(ABI.System.Uri), "Windows.Foundation.Uri", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(DataErrorsChangedEventArgs), typeof(ABI.System.ComponentModel.DataErrorsChangedEventArgs), "Microsoft.UI.Xaml.Data.DataErrorsChangedEventArgs", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(PropertyChangedEventArgs), typeof(ABI.System.ComponentModel.PropertyChangedEventArgs), "Microsoft.UI.Xaml.Data.PropertyChangedEventArgs", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(PropertyChangedEventHandler), typeof(ABI.System.ComponentModel.PropertyChangedEventHandler), "Microsoft.UI.Xaml.Data.PropertyChangedEventHandler");
RegisterCustomAbiTypeMappingNoLock(typeof(INotifyDataErrorInfo), typeof(ABI.System.ComponentModel.INotifyDataErrorInfo), "Microsoft.UI.Xaml.Data.INotifyDataErrorInfo");
RegisterCustomAbiTypeMappingNoLock(typeof(INotifyPropertyChanged), typeof(ABI.System.ComponentModel.INotifyPropertyChanged), "Microsoft.UI.Xaml.Data.INotifyPropertyChanged");
RegisterCustomAbiTypeMappingNoLock(typeof(ICommand), typeof(ABI.System.Windows.Input.ICommand), "Microsoft.UI.Xaml.Input.ICommand");
RegisterCustomAbiTypeMappingNoLock(typeof(IServiceProvider), typeof(ABI.System.IServiceProvider), "Microsoft.UI.Xaml.IXamlServiceProvider");
RegisterCustomAbiTypeMappingNoLock(typeof(EventHandler<>), typeof(ABI.System.EventHandler<>), "Windows.Foundation.EventHandler`1");
RegisterCustomAbiTypeMappingNoLock(typeof(KeyValuePair<,>), typeof(ABI.System.Collections.Generic.KeyValuePair<,>), "Windows.Foundation.Collections.IKeyValuePair`2");
RegisterCustomAbiTypeMappingNoLock(typeof(IEnumerable<>), typeof(ABI.System.Collections.Generic.IEnumerable<>), "Windows.Foundation.Collections.IIterable`1");
RegisterCustomAbiTypeMappingNoLock(typeof(IEnumerator<>), typeof(ABI.System.Collections.Generic.IEnumerator<>), "Windows.Foundation.Collections.IIterator`1");
RegisterCustomAbiTypeMappingNoLock(typeof(IList<>), typeof(ABI.System.Collections.Generic.IList<>), "Windows.Foundation.Collections.IVector`1");
RegisterCustomAbiTypeMappingNoLock(typeof(IReadOnlyList<>), typeof(ABI.System.Collections.Generic.IReadOnlyList<>), "Windows.Foundation.Collections.IVectorView`1");
RegisterCustomAbiTypeMappingNoLock(typeof(IDictionary<,>), typeof(ABI.System.Collections.Generic.IDictionary<,>), "Windows.Foundation.Collections.IMap`2");
RegisterCustomAbiTypeMappingNoLock(typeof(IReadOnlyDictionary<,>), typeof(ABI.System.Collections.Generic.IReadOnlyDictionary<,>), "Windows.Foundation.Collections.IMapView`2");
RegisterCustomAbiTypeMappingNoLock(typeof(IDisposable), typeof(ABI.System.IDisposable), "Windows.Foundation.IClosable");
RegisterCustomAbiTypeMappingNoLock(typeof(IEnumerable), typeof(ABI.System.Collections.IEnumerable), "Microsoft.UI.Xaml.Interop.IBindableIterable");
RegisterCustomAbiTypeMappingNoLock(typeof(IList), typeof(ABI.System.Collections.IList), "Microsoft.UI.Xaml.Interop.IBindableVector");
RegisterCustomAbiTypeMappingNoLock(typeof(INotifyCollectionChanged), typeof(ABI.System.Collections.Specialized.INotifyCollectionChanged), "Microsoft.UI.Xaml.Interop.INotifyCollectionChanged");
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedAction), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedAction), "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedAction");
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedEventArgs), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs), "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedEventArgs", isRuntimeClass: true);
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedEventHandler), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler), "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedEventHandler");
RegisterCustomAbiTypeMappingNoLock(typeof(Matrix3x2), typeof(ABI.System.Numerics.Matrix3x2), "Windows.Foundation.Numerics.Matrix3x2");
RegisterCustomAbiTypeMappingNoLock(typeof(Matrix4x4), typeof(ABI.System.Numerics.Matrix4x4), "Windows.Foundation.Numerics.Matrix4x4");
RegisterCustomAbiTypeMappingNoLock(typeof(Plane), typeof(ABI.System.Numerics.Plane), "Windows.Foundation.Numerics.Plane");
RegisterCustomAbiTypeMappingNoLock(typeof(Quaternion), typeof(ABI.System.Numerics.Quaternion), "Windows.Foundation.Numerics.Quaternion");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector2), typeof(ABI.System.Numerics.Vector2), "Windows.Foundation.Numerics.Vector2");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector3), typeof(ABI.System.Numerics.Vector3), "Windows.Foundation.Numerics.Vector3");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector4), typeof(ABI.System.Numerics.Vector4), "Windows.Foundation.Numerics.Vector4");
RegisterCustomAbiTypeMappingNoLock(typeof(EventHandler), typeof(ABI.System.EventHandler));
// TODO: Ideally we should not need these
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(IMap<,>), typeof(ABI.System.Collections.Generic.IDictionary<,>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(IVector<>), typeof(ABI.System.Collections.Generic.IList<>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(IMapView<,>), typeof(ABI.System.Collections.Generic.IReadOnlyDictionary<,>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(IVectorView<>), typeof(ABI.System.Collections.Generic.IReadOnlyList<>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(Microsoft.UI.Xaml.Interop.IBindableVector), typeof(ABI.System.Collections.IList));
#if NET
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(ICollection<>), typeof(ABI.System.Collections.Generic.ICollection<>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(IReadOnlyCollection<>), typeof(ABI.System.Collections.Generic.IReadOnlyCollection<>));
RegisterCustomTypeToHelperTypeMappingNoLock(typeof(ICollection), typeof(ABI.System.Collections.ICollection));
#endif
}
private static void RegisterCustomAbiTypeMapping(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type abiType,
string winrtTypeName,
bool isRuntimeClass = false)
{
rwlock.EnterWriteLock();
try
{
RegisterCustomAbiTypeMappingNoLock(publicType, abiType, winrtTypeName, isRuntimeClass);
}
finally
{
rwlock.ExitWriteLock();
}
}
private static void RegisterCustomTypeToHelperTypeMapping(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type helperType)
{
rwlock.EnterWriteLock();
try
{
CustomTypeToHelperTypeMappings.Add(publicType, helperType);
}
finally
{
rwlock.ExitWriteLock();
}
}
private static void RegisterCustomTypeToHelperTypeMappingNoLock(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type helperType)
{
CustomTypeToHelperTypeMappings.Add(publicType, helperType);
}
private static void RegisterCustomAbiTypeMappingNoLock(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type abiType,
string winrtTypeName,
bool isRuntimeClass = false)
{
CustomTypeToHelperTypeMappings.Add(publicType, abiType);
CustomAbiTypeToTypeMappings.Add(abiType, publicType);
CustomTypeToAbiTypeNameMappings.Add(publicType, winrtTypeName);
CustomAbiTypeNameToTypeMappings.Add(winrtTypeName, publicType);
if (isRuntimeClass)
{
ProjectedRuntimeClassNames.Add(winrtTypeName);
ProjectedCustomTypeRuntimeClasses.Add(publicType);
}
}
private static void RegisterCustomAbiTypeMapping(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type abiType)
{
rwlock.EnterWriteLock();
try
{
RegisterCustomAbiTypeMappingNoLock(publicType, abiType);
}
finally
{
rwlock.ExitWriteLock();
}
}
private static void RegisterCustomAbiTypeMappingNoLock(
Type publicType,
#if NET
[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
Type abiType)
{
CustomTypeToHelperTypeMappings.Add(publicType, abiType);
CustomAbiTypeToTypeMappings.Add(abiType, publicType);
}
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2068", Justification = "All types added to 'CustomTypeToHelperTypeMappings' have metadata explicitly preserved.")]
[return: DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type FindCustomHelperTypeMapping(Type publicType, bool filterToRuntimeClass = false)
{
rwlock.EnterReadLock();
try
{
if(filterToRuntimeClass && !ProjectedCustomTypeRuntimeClasses.Contains(publicType))
{
return null;
}
if (publicType.IsGenericType && !publicType.IsGenericTypeDefinition)
{
if (CustomTypeToHelperTypeMappings.TryGetValue(publicType, out Type specializedAbiType))
{
return specializedAbiType;
}
if (CustomTypeToHelperTypeMappings.TryGetValue(publicType.GetGenericTypeDefinition(), out Type abiTypeDefinition))
{
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot retrieve a helper type for public type '{publicType}'.");
}
#endif
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
return abiTypeDefinition.MakeGenericType(publicType.GetGenericArguments());
#pragma warning restore IL3050
}
return null;
}
return CustomTypeToHelperTypeMappings.TryGetValue(publicType, out Type abiType) ? abiType : null;
}
finally
{
rwlock.ExitReadLock();
}
}
public static Type FindCustomPublicTypeForAbiType(Type abiType)
{
rwlock.EnterReadLock();
try
{
if (abiType.IsGenericType)
{
if (CustomAbiTypeToTypeMappings.TryGetValue(abiType, out Type specializedPublicType))
{
return specializedPublicType;
}
if (CustomAbiTypeToTypeMappings.TryGetValue(abiType.GetGenericTypeDefinition(), out Type publicTypeDefinition))
{
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot retrieve a public type for ABI type '{abiType}'.");
}
#endif
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
return publicTypeDefinition.MakeGenericType(abiType.GetGenericArguments());
#pragma warning restore IL3050
}
return null;
}
return CustomAbiTypeToTypeMappings.TryGetValue(abiType, out Type publicType) ? publicType : null;
}
finally
{
rwlock.ExitReadLock();
}
}
public static Type FindCustomTypeForAbiTypeName(string abiTypeName)
{
rwlock.EnterReadLock();
try
{
return CustomAbiTypeNameToTypeMappings.TryGetValue(abiTypeName, out Type type) ? type : null;
}
finally
{
rwlock.ExitReadLock();
}
}
public static string FindCustomAbiTypeNameForType(Type type)
{
rwlock.EnterReadLock();
try
{
return CustomTypeToAbiTypeNameMappings.TryGetValue(type, out string typeName) ? typeName : null;
}
finally
{
rwlock.ExitReadLock();
}
}
private readonly static ConcurrentDictionary<Type, bool> IsTypeWindowsRuntimeTypeCache = new();
public static bool IsTypeWindowsRuntimeType(Type type)
{
return IsTypeWindowsRuntimeTypeCache.GetOrAdd(type, (type) =>
{
Type typeToTest = type;
if (typeToTest.IsArray)
{
typeToTest = typeToTest.GetElementType();
}
return IsTypeWindowsRuntimeTypeNoArray(typeToTest);
});
}
private static bool IsTypeWindowsRuntimeTypeNoArray(Type type)
{
if (type.IsConstructedGenericType)
{
if(IsTypeWindowsRuntimeTypeNoArray(type.GetGenericTypeDefinition()))
{
foreach (var arg in type.GetGenericArguments())
{
if (!IsTypeWindowsRuntimeTypeNoArray(arg))
{
return false;
}
}
return true;
}
return false;
}
return CustomTypeToAbiTypeNameMappings.ContainsKey(type)
|| type.IsPrimitive
|| type == typeof(string)
|| type == typeof(Guid)
|| type == typeof(object)
|| type.IsDefined(typeof(WindowsRuntimeTypeAttribute))
|| type.GetAuthoringMetadataType() != null;
}
// Use TryGetCompatibleWindowsRuntimeTypesForVariantType instead.
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055", Justification = "Calls to 'MakeGenericType' are always done with compatible types.")]
#endif
public static bool TryGetCompatibleWindowsRuntimeTypeForVariantType(Type type, out Type compatibleType)
{
compatibleType = null;
if (!type.IsConstructedGenericType)
{
throw new ArgumentException(nameof(type));
}
var definition = type.GetGenericTypeDefinition();
if (!IsTypeWindowsRuntimeTypeNoArray(definition))
{
return false;
}
#if NET
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
throw new NotSupportedException($"Cannot retrieve a compatible WinRT type for variant type '{type}'.");
}
#endif
var genericConstraints = definition.GetGenericArguments();
var genericArguments = type.GetGenericArguments();
var newArguments = new Type[genericArguments.Length];
for (int i = 0; i < genericArguments.Length; i++)
{
if (!IsTypeWindowsRuntimeTypeNoArray(genericArguments[i]))
{
bool argumentCovariant = (genericConstraints[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant;
if (argumentCovariant && !genericArguments[i].IsValueType)
{
newArguments[i] = typeof(object);
}
else
{
return false;
}
}
else
{
newArguments[i] = genericArguments[i];
}
}
#pragma warning disable IL3050 // https://github.com/dotnet/runtime/issues/97273
compatibleType = definition.MakeGenericType(newArguments);
#pragma warning restore IL3050
return true;
}
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
#if NET
[RequiresUnreferencedCode(AttributeMessages.GenericRequiresUnreferencedCodeMessage)]
#endif
private static HashSet<Type> GetCompatibleTypes(Type type, Stack<Type> typeStack)
{
HashSet<Type> compatibleTypes = new HashSet<Type>();
foreach (var iface in type.GetInterfaces())
{
if (IsTypeWindowsRuntimeTypeNoArray(iface))
{
compatibleTypes.Add(iface);
}
if (iface.IsConstructedGenericType
&& TryGetCompatibleWindowsRuntimeTypesForVariantType(iface, typeStack, out var compatibleIfaces))
{
compatibleTypes.UnionWith(compatibleIfaces);
}
}
Type baseType = type.BaseType;
while (baseType != null)
{
if (IsTypeWindowsRuntimeTypeNoArray(baseType))
{
compatibleTypes.Add(baseType);
}
baseType = baseType.BaseType;
}
return compatibleTypes;
}
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
internal static IEnumerable<Type> GetAllPossibleTypeCombinations(IEnumerable<IEnumerable<Type>> compatibleTypesPerGeneric, Type definition)
{
// Implementation adapted from https://stackoverflow.com/a/4424005
var accum = new List<Type>();
var compatibleTypesPerGenericArray = compatibleTypesPerGeneric.ToArray();
if (compatibleTypesPerGenericArray.Length > 0)
{
GetAllPossibleTypeCombinationsCore(
accum,
new Stack<Type>(),
compatibleTypesPerGenericArray,
compatibleTypesPerGenericArray.Length - 1);
}
return accum;
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "No members of the generic type are dynamically accessed other than for the attributes on it.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055", Justification = "Calls to 'MakeGenericType' are always done with compatible types.")]
#endif
void GetAllPossibleTypeCombinationsCore(List<Type> accum, Stack<Type> stack, IEnumerable<Type>[] compatibleTypes, int index)
{
foreach (var type in compatibleTypes[index])
{
stack.Push(type);
if (index == 0)
{
// IEnumerable on a System.Collections.Generic.Stack
// enumerates in order of removal (last to first).
// As a result, we get the correct ordering here.
accum.Add(definition.MakeGenericType(stack.ToArray()));
}
else
{
GetAllPossibleTypeCombinationsCore(accum, stack, compatibleTypes, index - 1);
}
stack.Pop();
}
}
}
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
#if NET
[RequiresUnreferencedCode(AttributeMessages.GenericRequiresUnreferencedCodeMessage)]
#endif
internal static bool TryGetCompatibleWindowsRuntimeTypesForVariantType(Type type, Stack<Type> typeStack, out IEnumerable<Type> compatibleTypes)
{
compatibleTypes = null;
if (!type.IsConstructedGenericType)
{
throw new ArgumentException(nameof(type));
}
var definition = type.GetGenericTypeDefinition();
if (!IsTypeWindowsRuntimeTypeNoArray(definition))
{
return false;
}
if (typeStack == null)
{
typeStack = new Stack<Type>();
}
else
{
if (typeStack.Contains(type))
{
return false;
}
}
typeStack.Push(type);
var genericConstraints = definition.GetGenericArguments();
var genericArguments = type.GetGenericArguments();
List<List<Type>> compatibleTypesPerGeneric = new List<List<Type>>();
for (int i = 0; i < genericArguments.Length; i++)
{
List<Type> compatibleTypesForGeneric = new List<Type>();
bool argumentCovariantObject = (genericConstraints[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant
&& !genericArguments[i].IsValueType;
if (IsTypeWindowsRuntimeTypeNoArray(genericArguments[i]))
{
compatibleTypesForGeneric.Add(genericArguments[i]);
}
else if (!argumentCovariantObject)
{
typeStack.Pop();
return false;
}
if (argumentCovariantObject)
{
compatibleTypesForGeneric.AddRange(GetCompatibleTypes(genericArguments[i], typeStack));
}
compatibleTypesPerGeneric.Add(compatibleTypesForGeneric);
}
typeStack.Pop();
compatibleTypes = GetAllPossibleTypeCombinations(compatibleTypesPerGeneric, definition);
return true;
}
private readonly static ConcurrentDictionary<Type, Type> DefaultInterfaceTypeCache = new();
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070",
Justification =
"The path using reflection to retrieve the default interface property is only used with legacy projections. " +
"Applications which make use of trimming will make use of updated projections and won't hit that code path.")]
#endif
internal static bool TryGetDefaultInterfaceTypeForRuntimeClassType(Type runtimeClass, out Type defaultInterface)
{
defaultInterface = DefaultInterfaceTypeCache.GetOrAdd(runtimeClass, (runtimeClass) =>
{
runtimeClass = runtimeClass.GetRuntimeClassCCWType() ?? runtimeClass;
ProjectedRuntimeClassAttribute attr = runtimeClass.GetCustomAttribute<ProjectedRuntimeClassAttribute>();
if (attr is null)
{
return null;
}
#if NET
// Using AOT requires using updated projections, which means we expect the type constructor to be used.
// The one taking a string for the property is not trim safe and is not used anymore by projections.
if (!RuntimeFeature.IsDynamicCodeCompiled)
{
return attr.DefaultInterface;
}
#endif
if (attr.DefaultInterface != null)
{
return attr.DefaultInterface;
}
// This path is only ever taken for .NET Standard legacy projections
if (attr.DefaultInterfaceProperty != null)
{
return runtimeClass.GetProperty(attr.DefaultInterfaceProperty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).PropertyType;
}
return null;
});
return defaultInterface != null;
}
internal static Type GetDefaultInterfaceTypeForRuntimeClassType(Type runtimeClass)
{
if (!TryGetDefaultInterfaceTypeForRuntimeClassType(runtimeClass, out Type defaultInterface))
{
throw new ArgumentException($"The provided type '{runtimeClass.FullName}' is not a WinRT projected runtime class.", nameof(runtimeClass));
}
return defaultInterface;
}
#if NET
#if NET8_0_OR_GREATER
[RequiresDynamicCode(AttributeMessages.MarshallingOrGenericInstantiationsRequiresDynamicCode)]
#endif
internal static Type GetAbiDelegateType(params Type[] typeArgs) => Expression.GetDelegateType(typeArgs);
#else
private class DelegateTypeComparer : IEqualityComparer<Type[]>
{
public bool Equals(Type[] x, Type[] y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(Type[] obj)
{
int hashCode = 0;
for (int idx = 0; idx < obj.Length; idx++)
{
hashCode ^= obj[idx].GetHashCode();
}
return hashCode;
}
}
private static readonly ConcurrentDictionary<Type[], Type> abiDelegateCache = new(new DelegateTypeComparer())
{
// IEnumerable
[new Type[] { typeof(void*), typeof(IntPtr).MakeByRefType(), typeof(int) }] = typeof(Interop._get_Current_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type).MakeByRefType(), typeof(int) }] = typeof(Interop._get_Current_Type),
// IList / IReadOnlyList
[new Type[] { typeof(void*), typeof(uint), typeof(IntPtr).MakeByRefType(), typeof(int) }] = typeof(Interop._get_At_IntPtr),
[new Type[] { typeof(void*), typeof(uint), typeof(ABI.System.Type).MakeByRefType(), typeof(int) }] = typeof(Interop._get_At_Type),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(uint).MakeByRefType(), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._index_Of_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(uint).MakeByRefType(), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._index_Of_Type),
[new Type[] { typeof(void*), typeof(uint), typeof(IntPtr), typeof(int) }] = typeof(Interop._set_At_IntPtr),
[new Type[] { typeof(void*), typeof(uint), typeof(ABI.System.Type), typeof(int) }] = typeof(Interop._set_At_Type),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(int) }] = typeof(Interop._append_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(int) }] = typeof(Interop._append_Type),
// IDictionary / IReadOnlyDictionary
[new Type[] { typeof(void*), typeof(IntPtr), typeof(IntPtr).MakeByRefType(), typeof(int) }] = typeof(Interop._lookup_IntPtr_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(ABI.System.Type).MakeByRefType(), typeof(int) }] = typeof(Interop._lookup_Type_Type),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(ABI.System.Type).MakeByRefType(), typeof(int) }] = typeof(Interop._lookup_IntPtr_Type),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(IntPtr).MakeByRefType(), typeof(int) }] = typeof(Interop._lookup_Type_IntPtr),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._has_key_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._has_key_Type),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(IntPtr), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._insert_IntPtr_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(ABI.System.Type), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._insert_Type_Type),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(ABI.System.Type), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._insert_IntPtr_Type),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(IntPtr), typeof(byte).MakeByRefType(), typeof(int) }] = typeof(Interop._insert_Type_IntPtr),
// EventHandler
[new Type[] { typeof(void*), typeof(IntPtr), typeof(IntPtr), typeof(int) }] = typeof(Interop._invoke_IntPtr_IntPtr),
[new Type[] { typeof(void*), typeof(IntPtr), typeof(ABI.System.Type), typeof(int) }] = typeof(Interop._invoke_IntPtr_Type),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(IntPtr), typeof(int) }] = typeof(Interop._invoke_Type_IntPtr),
[new Type[] { typeof(void*), typeof(ABI.System.Type), typeof(ABI.System.Type), typeof(int) }] = typeof(Interop._invoke_Type_Type),
};
public static void RegisterAbiDelegate(Type[] delegateSignature, Type delegateType)
{
abiDelegateCache.TryAdd(delegateSignature, delegateType);
}
// The .NET Standard projection can be used in both .NET Core and .NET Framework scenarios.
// With the latter, using Expression.GetDelegateType to create custom delegates with void* parameters
// doesn't seem to be supported. So we handle that by pregenerating all the ABI delegates that we need
// based on the WinMD and also by allowing apps to register their own if there are any
// that we couldn't detect (i.e. types passed as object in WinMD).
public static Type GetAbiDelegateType(params Type[] typeArgs)
{
if (abiDelegateCache.TryGetValue(typeArgs, out var delegateType))
{
return delegateType;
}
return Expression.GetDelegateType(typeArgs);
}
#endif
}
}