|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Concurrent; |
5 | 6 | using System.Diagnostics; |
6 | 7 | using System.IO; |
7 | 8 | using System.Linq; |
@@ -53,6 +54,8 @@ public static class RequestDelegateFactory |
53 | 54 | private static readonly MemberExpression StatusCodeExpr = Expression.Property(HttpResponseExpr, nameof(HttpResponse.StatusCode)); |
54 | 55 | private static readonly MemberExpression CompletedTaskExpr = Expression.Property(null, (PropertyInfo)GetMemberInfo<Func<Task>>(() => Task.CompletedTask)); |
55 | 56 |
|
| 57 | + private static ConcurrentDictionary<Type, MethodInfo?> TryParseMethodCache = new(); |
| 58 | + |
56 | 59 | /// <summary> |
57 | 60 | /// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="action"/>. |
58 | 61 | /// </summary> |
@@ -278,7 +281,6 @@ private static Expression CreateResponseWritingMethodCall(MethodInfo methodInfo, |
278 | 281 | // If we're calling TryParse and the WasTryParseFailureVariable indicates it failed, set a 400 StatusCode instead of calling the method. |
279 | 282 | private static Expression CreateTryParseCheckingResponseWritingMethodCall(MethodInfo methodInfo, Expression? target, Expression[] arguments) |
280 | 283 | { |
281 | | - |
282 | 284 | // { |
283 | 285 | // bool wasTryParseFailureVariable = false; |
284 | 286 | // string tempSourceString; |
@@ -553,9 +555,8 @@ private static MethodInfo GetEnumTryParseMethod() |
553 | 555 | throw new Exception("static bool System.Enum.TryParse<TEnum>(string? value, out TEnum result) does not exist!!?!?"); |
554 | 556 | } |
555 | 557 |
|
556 | | - // TODO: Cache this. |
557 | 558 | // TODO: Use InvariantCulture where possible? Or is CurrentCulture fine because it's more flexible? |
558 | | - private static MethodInfo? FindTryParseMethod(Type type) |
| 559 | + private static MethodInfo? FindTryParseMethodUncached(Type type) |
559 | 560 | { |
560 | 561 | if (type.IsEnum) |
561 | 562 | { |
@@ -585,6 +586,11 @@ private static MethodInfo GetEnumTryParseMethod() |
585 | 586 | return null; |
586 | 587 | } |
587 | 588 |
|
| 589 | + private static MethodInfo? FindTryParseMethod(Type type) |
| 590 | + { |
| 591 | + return TryParseMethodCache.GetOrAdd(type, FindTryParseMethodUncached); |
| 592 | + } |
| 593 | + |
588 | 594 | private static bool HasTryParseMethod(ParameterInfo parameter) |
589 | 595 | { |
590 | 596 | var nonNullableParameterType = Nullable.GetUnderlyingType(parameter.ParameterType) ?? parameter.ParameterType; |
|
0 commit comments