forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterBindingMethodCache.cs
721 lines (623 loc) · 33.6 KB
/
ParameterBindingMethodCache.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Internal;
using static Microsoft.AspNetCore.Http.ParameterBindingMethodCache.SharedExpressions;
#nullable enable
namespace Microsoft.AspNetCore.Http;
[RequiresUnreferencedCode("Uses unbounded Reflection to inspect property types.")]
internal sealed class ParameterBindingMethodCache
{
private static readonly MethodInfo ConvertValueTaskMethod = typeof(ParameterBindingMethodCache).GetMethod(nameof(ConvertValueTask), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo ConvertValueTaskOfNullableResultMethod = typeof(ParameterBindingMethodCache).GetMethod(nameof(ConvertValueTaskOfNullableResult), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo BindAsyncMethod = typeof(ParameterBindingMethodCache).GetMethod(nameof(BindAsync), BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo UriTryCreateMethod = typeof(Uri).GetMethod(nameof(Uri.TryCreate), BindingFlags.Public | BindingFlags.Static, new[] { typeof(string), typeof(UriKind), typeof(Uri).MakeByRefType() })!;
// work around https://github.com/dotnet/runtime/issues/81864 by splitting these into a separate class.
internal static class SharedExpressions
{
internal static readonly ParameterExpression TempSourceStringExpr = Expression.Variable(typeof(string), "tempSourceString");
internal static readonly ParameterExpression HttpContextExpr = Expression.Parameter(typeof(HttpContext), "httpContext");
}
private readonly MethodInfo _enumTryParseMethod;
private readonly bool _throwOnInvalidMethod;
// Since this is shared source, the cache won't be shared between RequestDelegateFactory and the ApiDescriptionProvider sadly :(
private readonly ConcurrentDictionary<Type, Func<ParameterExpression, Expression, Expression>?> _stringMethodCallCache = new();
private readonly ConcurrentDictionary<Type, (Func<ParameterInfo, Expression>?, int)> _bindAsyncMethodCallCache = new();
private readonly ConcurrentDictionary<Type, (ConstructorInfo?, ConstructorParameter[])> _constructorCache = new();
// If IsDynamicCodeSupported is false, we can't use the static Enum.TryParse<T> since there's no easy way for
// this code to generate the specific instantiation for any enums used
public ParameterBindingMethodCache(bool throwOnInvalidMethod = true)
: this(preferNonGenericEnumParseOverload: !RuntimeFeature.IsDynamicCodeSupported,
throwOnInvalidMethod)
{
}
// This is for testing
public ParameterBindingMethodCache(bool preferNonGenericEnumParseOverload, bool throwOnInvalidMethod = true)
{
_enumTryParseMethod = GetEnumTryParseMethod(preferNonGenericEnumParseOverload);
_throwOnInvalidMethod = throwOnInvalidMethod;
}
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
[RequiresDynamicCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
public bool HasTryParseMethod(Type type)
{
var nonNullableParameterType = Nullable.GetUnderlyingType(type) ?? type;
return FindTryParseMethod(nonNullableParameterType) is not null;
}
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
[RequiresDynamicCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
public bool HasBindAsyncMethod(ParameterInfo parameter) =>
FindBindAsyncMethod(parameter).Expression is not null;
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
[RequiresDynamicCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
public Func<ParameterExpression, Expression, Expression>? FindTryParseMethod(Type type)
{
// This method is used to find TryParse methods from .NET types using reflection. It's used at app runtime.
// Routing analyzers also detect TryParse methods when calculating what types are valid in routes.
// Changes here to support new types should be reflected in analyzers.
Func<ParameterExpression, Expression, Expression>? Finder(Type type)
{
MethodInfo? methodInfo;
if (type.IsEnum)
{
if (_enumTryParseMethod.IsGenericMethod)
{
methodInfo = _enumTryParseMethod.MakeGenericMethod(type);
return (expression, formatProvider) => Expression.Call(methodInfo!, TempSourceStringExpr, expression);
}
return (expression, formatProvider) =>
{
var enumAsObject = Expression.Variable(typeof(object), "enumAsObject");
var success = Expression.Variable(typeof(bool), "success");
// object enumAsObject;
// bool success;
// success = Enum.TryParse(type, tempSourceString, out enumAsObject);
// parsedValue = success ? (Type)enumAsObject : default;
// return success;
return Expression.Block(new[] { success, enumAsObject },
Expression.Assign(success, Expression.Call(_enumTryParseMethod, Expression.Constant(type), TempSourceStringExpr, enumAsObject)),
Expression.Assign(expression,
Expression.Condition(success, Expression.Convert(enumAsObject, type), Expression.Default(type))),
success);
};
}
if (type == typeof(Uri))
{
// UriKind.RelativeOrAbsolute is also used by UriTypeConverter which is used in MVC.
return (expression, formatProvider) => Expression.Call(
UriTryCreateMethod,
TempSourceStringExpr,
Expression.Constant(UriKind.RelativeOrAbsolute),
expression);
}
if (TryGetDateTimeTryParseMethod(type, out methodInfo))
{
// We generate `DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces ` to
// support parsing types into the UTC timezone for DateTime. We don't assume the timezone
// on the original value which will cause the parser to set the `Kind` property on the
// `DateTime` as `Unspecified` indicating that it was parsed from an ambiguous timezone.
//
// `DateTimeOffset`s are always in UTC and don't allow specifying an `Unspecific` kind.
// For this, we always assume that the original value is already in UTC to avoid resolving
// the offset incorrectly depending on the timezone of the machine. We don't bother mapping
// it to UTC in this case. In the event that the original timestamp is not in UTC, it's offset
// value will be maintained.
//
// DateOnly and TimeOnly types do not support conversion to Utc so we
// default to `DateTimeStyles.AllowWhiteSpaces`.
var dateTimeStyles = type switch
{
Type t when t == typeof(DateTime) => DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces,
Type t when t == typeof(DateTimeOffset) => DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces,
_ => DateTimeStyles.AllowWhiteSpaces
};
return (expression, formatProvider) => Expression.Call(
methodInfo!,
TempSourceStringExpr,
formatProvider,
Expression.Constant(dateTimeStyles),
expression);
}
if (TryGetNumberStylesTryGetMethod(type, out methodInfo, out var numberStyle))
{
return (expression, formatProvider) => Expression.Call(
methodInfo!,
TempSourceStringExpr,
Expression.Constant(numberStyle),
formatProvider,
expression);
}
methodInfo = GetStaticMethodFromHierarchy(type, "TryParse", new[] { typeof(string), typeof(IFormatProvider), type.MakeByRefType() }, ValidateReturnType);
if (methodInfo is not null)
{
return (expression, formatProvider) => Expression.Call(
methodInfo,
TempSourceStringExpr,
formatProvider,
expression);
}
methodInfo = GetStaticMethodFromHierarchy(type, "TryParse", new[] { typeof(string), type.MakeByRefType() }, ValidateReturnType);
if (methodInfo is not null)
{
return (expression, formatProvider) => Expression.Call(methodInfo, TempSourceStringExpr, expression);
}
if (_throwOnInvalidMethod && GetAnyMethodFromHierarchy(type, "TryParse") is MethodInfo invalidMethod)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"TryParse method found on {TypeNameHelper.GetTypeDisplayName(type, fullName: false)} with incorrect format. Must be a static method with format");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"bool TryParse(string, IFormatProvider, out {TypeNameHelper.GetTypeDisplayName(type, fullName: false)})");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"bool TryParse(string, out {TypeNameHelper.GetTypeDisplayName(type, fullName: false)})");
stringBuilder.AppendLine("but found");
stringBuilder.Append(invalidMethod.IsStatic ? "static " : "not-static ");
stringBuilder.Append(invalidMethod.ToString());
throw new InvalidOperationException(stringBuilder.ToString());
}
return null;
static bool ValidateReturnType(MethodInfo methodInfo)
{
return methodInfo.ReturnType.Equals(typeof(bool));
}
}
return _stringMethodCallCache.GetOrAdd(type, Finder);
}
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
[RequiresDynamicCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
public (Expression? Expression, int ParamCount) FindBindAsyncMethod(ParameterInfo parameter)
{
(Func<ParameterInfo, Expression>?, int) Finder(Type nonNullableParameterType)
{
var hasParameterInfo = true;
var methodInfo = GetIBindableFromHttpContextMethod(nonNullableParameterType);
if (methodInfo is null)
{
// There should only be one BindAsync method with these parameters since C# does not allow overloading on return type.
methodInfo = GetStaticMethodFromHierarchy(nonNullableParameterType, "BindAsync", new[] { typeof(HttpContext), typeof(ParameterInfo) }, ValidateReturnType);
if (methodInfo is null)
{
hasParameterInfo = false;
methodInfo = GetStaticMethodFromHierarchy(nonNullableParameterType, "BindAsync", new[] { typeof(HttpContext) }, ValidateReturnType);
}
}
// We're looking for a method with the following signatures:
// public static ValueTask<{type}> BindAsync(HttpContext context, ParameterInfo parameter)
// public static ValueTask<Nullable<{type}>> BindAsync(HttpContext context, ParameterInfo parameter)
if (methodInfo is not null)
{
var valueTaskResultType = methodInfo.ReturnType.GetGenericArguments()[0];
// ValueTask<{type}>?
if (valueTaskResultType == nonNullableParameterType)
{
return ((parameter) =>
{
MethodCallExpression typedCall;
if (hasParameterInfo)
{
// parameter is being intentionally shadowed. We never want to use the outer ParameterInfo inside
// this Func because the ParameterInfo varies after it's been cached for a given parameter type.
typedCall = Expression.Call(methodInfo, HttpContextExpr, Expression.Constant(parameter));
}
else
{
typedCall = Expression.Call(methodInfo, HttpContextExpr);
}
return Expression.Call(ConvertValueTaskMethod.MakeGenericMethod(nonNullableParameterType), typedCall);
}, hasParameterInfo ? 2 : 1);
}
// ValueTask<Nullable<{type}>>?
else if (valueTaskResultType.IsGenericType &&
valueTaskResultType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
valueTaskResultType.GetGenericArguments()[0] == nonNullableParameterType)
{
return ((parameter) =>
{
MethodCallExpression typedCall;
if (hasParameterInfo)
{
// parameter is being intentionally shadowed. We never want to use the outer ParameterInfo inside
// this Func because the ParameterInfo varies after it's been cached for a given parameter type.
typedCall = Expression.Call(methodInfo, HttpContextExpr, Expression.Constant(parameter));
}
else
{
typedCall = Expression.Call(methodInfo, HttpContextExpr);
}
return Expression.Call(ConvertValueTaskOfNullableResultMethod.MakeGenericMethod(nonNullableParameterType), typedCall);
}, hasParameterInfo ? 2 : 1);
}
}
if (_throwOnInvalidMethod && GetAnyMethodFromHierarchy(nonNullableParameterType, "BindAsync") is MethodInfo invalidBindMethod)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"BindAsync method found on {TypeNameHelper.GetTypeDisplayName(nonNullableParameterType, fullName: false)} with incorrect format. Must be a static method with format");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"ValueTask<{TypeNameHelper.GetTypeDisplayName(nonNullableParameterType, fullName: false)}> BindAsync(HttpContext context, ParameterInfo parameter)");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"ValueTask<{TypeNameHelper.GetTypeDisplayName(nonNullableParameterType, fullName: false)}> BindAsync(HttpContext context)");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"ValueTask<{TypeNameHelper.GetTypeDisplayName(nonNullableParameterType, fullName: false)}?> BindAsync(HttpContext context, ParameterInfo parameter)");
stringBuilder.AppendLine(CultureInfo.InvariantCulture, $"ValueTask<{TypeNameHelper.GetTypeDisplayName(nonNullableParameterType, fullName: false)}?> BindAsync(HttpContext context)");
stringBuilder.AppendLine("but found");
stringBuilder.Append(invalidBindMethod.IsStatic ? "static " : "not-static");
stringBuilder.Append(invalidBindMethod.ToString());
throw new InvalidOperationException(stringBuilder.ToString());
}
return (null, 0);
}
var nonNullableParameterType = Nullable.GetUnderlyingType(parameter.ParameterType) ?? parameter.ParameterType;
var (method, paramCount) = _bindAsyncMethodCallCache.GetOrAdd(nonNullableParameterType, Finder);
return (method?.Invoke(parameter), paramCount);
static bool ValidateReturnType(MethodInfo methodInfo)
{
return methodInfo.ReturnType.IsGenericType &&
methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>);
}
}
public (ConstructorInfo?, ConstructorParameter[]) FindConstructor(Type type)
{
static (ConstructorInfo? constructor, ConstructorParameter[] parameters) Finder(Type type)
{
var constructor = GetConstructor(type);
if (constructor is null || constructor.GetParameters().Length == 0)
{
return (constructor, Array.Empty<ConstructorParameter>());
}
var properties = type.GetProperties();
var lookupTable = new Dictionary<ParameterLookupKey, PropertyInfo>(properties.Length);
for (var i = 0; i < properties.Length; i++)
{
lookupTable.Add(new ParameterLookupKey(properties[i].Name, properties[i].PropertyType), properties[i]);
}
// This behavior diverge from the JSON serialization
// since we don't have an attribute, eg. JsonConstructor,
// we need to be very restrictive about the ctor
// and only accept if the parameterized ctor has
// only arguments that we can match (Type and Name)
// with a public property.
var parameters = constructor.GetParameters();
var parametersWithPropertyInfo = new ConstructorParameter[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
var key = new ParameterLookupKey(parameters[i].Name!, parameters[i].ParameterType);
if (!lookupTable.TryGetValue(key, out var property))
{
throw new InvalidOperationException(
$"The public parameterized constructor must contain only parameters that match the declared public properties for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.");
}
parametersWithPropertyInfo[i] = new ConstructorParameter(parameters[i], property);
}
return (constructor, parametersWithPropertyInfo);
}
return _constructorCache.GetOrAdd(type, Finder);
}
private static ConstructorInfo? GetConstructor(Type type)
{
if (type.IsAbstract)
{
throw new InvalidOperationException($"The abstract type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}' is not supported.");
}
var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
// if only one constructor is declared
// we will use it to try match the properties
if (constructors.Length == 1)
{
return constructors[0];
}
// We will try to get the parameterless ctor
// as priority before visit the others
var parameterlessConstructor = constructors.SingleOrDefault(c => c.GetParameters().Length == 0);
if (parameterlessConstructor is not null)
{
return parameterlessConstructor;
}
// If a parameterized constructors is not found at this point
// we will use a default constructor that is always available
// for value types.
if (type.IsValueType)
{
return null;
}
// We don't have an attribute, similar to JsonConstructor, to
// disambiguate ctors, so, we will throw if more than one
// ctor is defined without a parameterless constructor.
// Eg.:
// public class X
// {
// public X(int foo)
// public X(int foo, int bar)
// ...
// }
if (parameterlessConstructor is null && constructors.Length > 1)
{
throw new InvalidOperationException($"Only a single public parameterized constructor is allowed for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.");
}
throw new InvalidOperationException($"No public parameterless constructor found for type '{TypeNameHelper.GetTypeDisplayName(type, fullName: false)}'.");
}
[RequiresDynamicCode("MakeGenericMethod is possible used with ValueTypes and isn't compatible with AOT.")]
private static MethodInfo? GetIBindableFromHttpContextMethod(Type type)
{
// Check if parameter is bindable via static abstract method on IBindableFromHttpContext<TSelf>
foreach (var i in type.GetInterfaces())
{
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IBindableFromHttpContext<>) && i.GetGenericArguments()[0] == type)
{
return BindAsyncMethod.MakeGenericMethod(type);
}
}
return null;
}
private static ValueTask<TValue?> BindAsync<TValue>(HttpContext httpContext, ParameterInfo parameter)
where TValue : class?, IBindableFromHttpContext<TValue>
{
return TValue.BindAsync(httpContext, parameter);
}
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
private MethodInfo? GetStaticMethodFromHierarchy(Type type, string name, Type[] parameterTypes, Func<MethodInfo, bool> validateReturnType)
{
bool IsMatch(MethodInfo? method) => method is not null && !method.IsAbstract && validateReturnType(method);
var methodInfo = type.GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy, parameterTypes);
if (IsMatch(methodInfo))
{
return methodInfo;
}
var candidateInterfaceMethodInfo = default(MethodInfo);
// Check all interfaces for implementations. Fail if there are duplicates.
foreach (var implementedInterface in type.GetInterfaces())
{
var interfaceMethod = implementedInterface.GetMethod(name, BindingFlags.Public | BindingFlags.Static, parameterTypes);
if (IsMatch(interfaceMethod))
{
if (candidateInterfaceMethodInfo is not null)
{
if (_throwOnInvalidMethod)
{
throw new InvalidOperationException($"{TypeNameHelper.GetTypeDisplayName(type, fullName: false)} implements multiple interfaces defining a static {interfaceMethod} method causing ambiguity.");
}
return null;
}
candidateInterfaceMethodInfo = interfaceMethod;
}
}
return candidateInterfaceMethodInfo;
}
[RequiresUnreferencedCode("Performs reflection on type hierarchy. This cannot be statically analyzed.")]
private static MethodInfo? GetAnyMethodFromHierarchy(Type type, string name)
{
// Find first incorrectly formatted method
var methodInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.FirstOrDefault(methodInfo => methodInfo.Name == name);
if (methodInfo is not null)
{
return methodInfo;
}
foreach (var implementedInterface in type.GetInterfaces())
{
var interfaceMethod = implementedInterface.GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
if (interfaceMethod is not null)
{
return interfaceMethod;
}
}
return null;
}
private static MethodInfo GetEnumTryParseMethod(bool preferNonGenericEnumParseOverload)
{
MethodInfo? methodInfo = null;
if (preferNonGenericEnumParseOverload)
{
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(Type), typeof(string), typeof(object).MakeByRefType() });
}
else
{
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
genericParameterCount: 1,
new[] { typeof(string), Type.MakeGenericMethodParameter(0).MakeByRefType() });
}
if (methodInfo is null)
{
Debug.Fail("No suitable System.Enum.TryParse method found.");
throw new MissingMethodException("No suitable System.Enum.TryParse method found.");
}
return methodInfo!;
}
private static bool TryGetDateTimeTryParseMethod(Type type, [NotNullWhen(true)] out MethodInfo? methodInfo)
{
methodInfo = null;
if (type == typeof(DateTime))
{
methodInfo = typeof(DateTime).GetMethod(
nameof(DateTime.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(IFormatProvider), typeof(DateTimeStyles), typeof(DateTime).MakeByRefType() });
}
else if (type == typeof(DateTimeOffset))
{
methodInfo = typeof(DateTimeOffset).GetMethod(
nameof(DateTimeOffset.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(IFormatProvider), typeof(DateTimeStyles), typeof(DateTimeOffset).MakeByRefType() });
}
else if (type == typeof(DateOnly))
{
methodInfo = typeof(DateOnly).GetMethod(
nameof(DateOnly.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(IFormatProvider), typeof(DateTimeStyles), typeof(DateOnly).MakeByRefType() });
}
else if (type == typeof(TimeOnly))
{
methodInfo = typeof(TimeOnly).GetMethod(
nameof(TimeOnly.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(IFormatProvider), typeof(DateTimeStyles), typeof(TimeOnly).MakeByRefType() });
}
return methodInfo != null;
}
private static bool TryGetNumberStylesTryGetMethod(Type type, [NotNullWhen(true)] out MethodInfo? method, [NotNullWhen(true)] out NumberStyles? numberStyles)
{
method = null;
numberStyles = NumberStyles.Integer;
if (type == typeof(long))
{
method = typeof(long).GetMethod(
nameof(long.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() });
}
else if (type == typeof(ulong))
{
method = typeof(ulong).GetMethod(
nameof(ulong.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(ulong).MakeByRefType() });
}
else if (type == typeof(int))
{
method = typeof(int).GetMethod(
nameof(int.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(int).MakeByRefType() });
}
else if (type == typeof(uint))
{
method = typeof(uint).GetMethod(
nameof(uint.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(uint).MakeByRefType() });
}
else if (type == typeof(short))
{
method = typeof(short).GetMethod(
nameof(short.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(short).MakeByRefType() });
}
else if (type == typeof(ushort))
{
method = typeof(ushort).GetMethod(
nameof(ushort.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(ushort).MakeByRefType() });
}
else if (type == typeof(byte))
{
method = typeof(byte).GetMethod(
nameof(byte.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(byte).MakeByRefType() });
}
else if (type == typeof(sbyte))
{
method = typeof(sbyte).GetMethod(
nameof(sbyte.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(sbyte).MakeByRefType() });
}
else if (type == typeof(double))
{
method = typeof(double).GetMethod(
nameof(double.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(double).MakeByRefType() });
numberStyles = NumberStyles.AllowThousands | NumberStyles.Float;
}
else if (type == typeof(float))
{
method = typeof(float).GetMethod(
nameof(float.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(float).MakeByRefType() });
numberStyles = NumberStyles.AllowThousands | NumberStyles.Float;
}
else if (type == typeof(Half))
{
method = typeof(Half).GetMethod(
nameof(Half.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(Half).MakeByRefType() });
numberStyles = NumberStyles.AllowThousands | NumberStyles.Float;
}
else if (type == typeof(decimal))
{
method = typeof(decimal).GetMethod(
nameof(decimal.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(decimal).MakeByRefType() });
numberStyles = NumberStyles.Number;
}
else if (type == typeof(IntPtr))
{
method = typeof(IntPtr).GetMethod(
nameof(IntPtr.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(IntPtr).MakeByRefType() });
}
else if (type == typeof(BigInteger))
{
method = typeof(BigInteger).GetMethod(
nameof(BigInteger.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(BigInteger).MakeByRefType() });
}
return method != null;
}
private static ValueTask<object?> ConvertValueTask<T>(ValueTask<T> typedValueTask)
{
if (typedValueTask.IsCompletedSuccessfully)
{
var result = typedValueTask.GetAwaiter().GetResult();
return new ValueTask<object?>(result);
}
static async ValueTask<object?> ConvertAwaited(ValueTask<T> typedValueTask) => await typedValueTask;
return ConvertAwaited(typedValueTask);
}
private static ValueTask<object?> ConvertValueTaskOfNullableResult<T>(ValueTask<Nullable<T>> typedValueTask) where T : struct
{
if (typedValueTask.IsCompletedSuccessfully)
{
var result = typedValueTask.GetAwaiter().GetResult();
return new ValueTask<object?>(result);
}
static async ValueTask<object?> ConvertAwaited(ValueTask<Nullable<T>> typedValueTask) => await typedValueTask;
return ConvertAwaited(typedValueTask);
}
private sealed class ParameterLookupKey
{
public ParameterLookupKey(string name, Type type)
{
Name = name;
Type = type;
}
public string Name { get; }
public Type Type { get; }
public override int GetHashCode()
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(Name);
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
Debug.Assert(obj is ParameterLookupKey);
var other = (ParameterLookupKey)obj;
return Type == other.Type && string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase);
}
}
internal sealed class ConstructorParameter
{
public ConstructorParameter(ParameterInfo parameter, PropertyInfo propertyInfo)
{
ParameterInfo = parameter;
PropertyInfo = propertyInfo;
}
public ParameterInfo ParameterInfo { get; }
public PropertyInfo PropertyInfo { get; }
}
}