-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathType.cs
383 lines (314 loc) · 12 KB
/
Type.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using WinRT;
namespace ABI.System
{
[WindowsRuntimeType("Windows.Foundation.UniversalApiContract")]
internal enum TypeKind : int
{
Primitive,
Metadata,
Custom
}
#if EMBED
internal
#else
public
#endif
struct Type
{
private IntPtr Name;
private TypeKind Kind;
public struct Marshaler
{
internal MarshalString Name;
internal TypeKind Kind;
internal void Dispose()
{
MarshalString.DisposeMarshaler(Name);
}
}
public ref struct Pinnable
{
internal MarshalString.Pinnable Name;
internal TypeKind Kind;
public Pinnable(global::System.Type type)
{
var abi = ToAbi(type);
Name = new MarshalString.Pinnable(abi.Name);
Kind = abi.Kind;
}
public ref readonly char GetPinnableReference()
{
return ref Name.GetPinnableReference();
}
}
private static (String Name, TypeKind Kind) ToAbi(global::System.Type value)
{
#if NET
if (value is FakeMetadataType fakeMetadataType)
{
return (fakeMetadataType.FullName, TypeKind.Metadata);
}
#endif
TypeKind kind = TypeKind.Custom;
if (value is not null)
{
if (value.IsPrimitive)
{
kind = TypeKind.Primitive;
}
else if (value == typeof(object) || value == typeof(string) || value == typeof(Guid) || value == typeof(System.Type))
{
kind = TypeKind.Metadata;
}
else if (Projections.IsTypeWindowsRuntimeType(value))
{
kind = TypeKind.Metadata;
}
}
return (GetNameForTypeCached(value, kind == TypeKind.Custom), kind);
}
private static readonly ConcurrentDictionary<global::System.Type, string> typeNameCache = new();
private static string GetNameForTypeCached(global::System.Type value, bool customKind)
{
if (customKind)
{
return typeNameCache.GetOrAdd(value, (type) => type.AssemblyQualifiedName);
}
else
{
return typeNameCache.GetOrAdd(value, (type) => TypeNameSupport.GetNameForType(type, TypeNameGenerationFlags.None));
}
}
public static Marshaler CreateMarshaler(global::System.Type value)
{
var abi = ToAbi(value);
return new Marshaler
{
Name = MarshalString.CreateMarshaler(abi.Name),
Kind = abi.Kind
};
}
public static Type GetAbi(ref Pinnable p)
{
return new Type
{
Name = MarshalString.GetAbi(ref p.Name),
Kind = p.Kind
};
}
public static Type GetAbi(Marshaler m)
{
return new Type
{
Name = MarshalString.GetAbi(m.Name),
Kind = m.Kind
};
}
#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "Any types which are trimmed are not used by managed user code and there is fallback logic to handle that.")]
#endif
public static global::System.Type FromAbi(Type value)
{
string name = MarshalString.FromAbi(value.Name);
if (string.IsNullOrEmpty(name))
{
return null;
}
if (value.Kind == TypeKind.Custom)
{
return global::System.Type.GetType(name);
}
var type = TypeNameSupport.FindTypeByNameCached(name);
#if NET
// The type might have been trimmed, represent it with a fake type if requested
// by the the Xaml metadata provider. Given there are no C# references to it, it shouldn't
// be used, but if it is, an exception will be thrown.
if (type == null && value.Kind == TypeKind.Metadata)
{
type = FakeMetadataType.GetFakeMetadataType(name);
}
#endif
return type;
}
public static unsafe void CopyAbi(Marshaler arg, IntPtr dest) =>
*(Type*)dest.ToPointer() = GetAbi(arg);
public static Type FromManaged(global::System.Type value)
{
var abi = ToAbi(value);
return new Type
{
Name = MarshalString.FromManaged(abi.Name),
Kind = abi.Kind
};
}
public static unsafe void CopyManaged(global::System.Type arg, IntPtr dest) =>
*(Type*)dest.ToPointer() = FromManaged(arg);
public static void DisposeMarshaler(Marshaler m) { m.Dispose(); }
public static void DisposeAbi(Type abi) { MarshalString.DisposeAbi(abi.Name); }
public static string GetGuidSignature()
{
return "struct(Windows.UI.Xaml.Interop.TypeName;string;enum(Windows.UI.Xaml.Interop.TypeKind;i4))";
}
}
// Restricting to NET5 or greater as TypeInfo doesn't expose a constructor on .NET Standard 2.0 and we only need this
// for WinUI scenarios.
#if NET
internal sealed class FakeMetadataType : global::System.Reflection.TypeInfo
{
private static readonly ConcurrentDictionary<string, FakeMetadataType> fakeMetadataTypeCache = new(StringComparer.Ordinal);
private readonly string fullName;
private FakeMetadataType(string fullName)
{
this.fullName = fullName;
}
internal static FakeMetadataType GetFakeMetadataType(string name)
{
return fakeMetadataTypeCache.GetOrAdd(name, (name) => new FakeMetadataType(name));
}
public override Assembly Assembly => throw new InvalidOperationException();
public override string AssemblyQualifiedName => throw new InvalidOperationException();
public override global::System.Type BaseType => throw new InvalidOperationException();
public override string FullName => fullName;
public override Guid GUID => throw new InvalidOperationException();
public override Module Module => throw new InvalidOperationException();
public override string Namespace => throw new InvalidOperationException();
public override global::System.Type UnderlyingSystemType => throw new InvalidOperationException();
public override string Name => throw new InvalidOperationException();
public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override object[] GetCustomAttributes(bool inherit)
{
throw new InvalidOperationException();
}
public override object[] GetCustomAttributes(global::System.Type attributeType, bool inherit)
{
throw new InvalidOperationException();
}
public override global::System.Type GetElementType()
{
throw new InvalidOperationException();
}
public override EventInfo GetEvent(string name, BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override EventInfo[] GetEvents(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override FieldInfo GetField(string name, BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override FieldInfo[] GetFields(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
#if NET6_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
#endif
public override global::System.Type GetInterface(string name, bool ignoreCase)
{
throw new InvalidOperationException();
}
public override global::System.Type[] GetInterfaces()
{
throw new InvalidOperationException();
}
public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override global::System.Type GetNestedType(string name, BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override global::System.Type[] GetNestedTypes(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override PropertyInfo[] GetProperties(BindingFlags bindingAttr)
{
throw new InvalidOperationException();
}
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
throw new InvalidOperationException();
}
public override bool IsDefined(global::System.Type attributeType, bool inherit)
{
throw new InvalidOperationException();
}
protected override TypeAttributes GetAttributeFlagsImpl()
{
throw new InvalidOperationException();
}
protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, global::System.Type[] types, ParameterModifier[] modifiers)
{
throw new InvalidOperationException();
}
protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, global::System.Type[] types, ParameterModifier[] modifiers)
{
throw new InvalidOperationException();
}
protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, global::System.Type returnType, global::System.Type[] types, ParameterModifier[] modifiers)
{
throw new InvalidOperationException();
}
protected override bool HasElementTypeImpl()
{
throw new InvalidOperationException();
}
protected override bool IsArrayImpl()
{
throw new InvalidOperationException();
}
protected override bool IsByRefImpl()
{
throw new InvalidOperationException();
}
protected override bool IsCOMObjectImpl()
{
throw new InvalidOperationException();
}
protected override bool IsPointerImpl()
{
throw new InvalidOperationException();
}
protected override bool IsPrimitiveImpl()
{
throw new InvalidOperationException();
}
public override string ToString()
{
return fullName;
}
public override bool Equals(object o)
{
return ReferenceEquals(this, o);
}
public override bool Equals(global::System.Type o)
{
return ReferenceEquals(this, o);
}
public override int GetHashCode()
{
return fullName.GetHashCode();
}
}
#endif
}