Skip to content

Commit 3fc546e

Browse files
committed
cleanup
1 parent 4060f1b commit 3fc546e

File tree

2 files changed

+27
-52
lines changed

2 files changed

+27
-52
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ private static Expression CreateTryParseCheckingResponseWritingMethodCall(Method
284284
// // Assume "[FromRoute] int id" is the first parameter.
285285
//
286286
// tempSourceString = httpContext.RequestValue["id"];
287-
//
288287
// int param1 = tempSourceString == null ? default :
289288
// {
290289
// int parsedValue = default;
@@ -552,39 +551,39 @@ private static MethodInfo GetEnumTryParseMethod()
552551
}
553552

554553
// TODO: Use InvariantCulture where possible? Or is CurrentCulture fine because it's more flexible?
555-
private static MethodInfo? FindTryParseMethodUncached(Type type)
554+
private static MethodInfo? FindTryParseMethod(Type type)
556555
{
557-
if (type.IsEnum)
558-
{
559-
return EnumTryParseMethod.MakeGenericMethod(type);
560-
}
561-
562-
var staticMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
563-
564-
foreach (var method in staticMethods)
556+
static MethodInfo? Finder(Type type)
565557
{
566-
if (method.Name != "TryParse" || method.ReturnType != typeof(bool))
558+
if (type.IsEnum)
567559
{
568-
continue;
560+
return EnumTryParseMethod.MakeGenericMethod(type);
569561
}
570562

571-
var tryParseParameters = method.GetParameters();
563+
var staticMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
572564

573-
if (tryParseParameters.Length == 2 &&
574-
tryParseParameters[0].ParameterType == typeof(string) &&
575-
tryParseParameters[1].IsOut &&
576-
tryParseParameters[1].ParameterType == type.MakeByRefType())
565+
foreach (var method in staticMethods)
577566
{
578-
return method;
567+
if (method.Name != "TryParse" || method.ReturnType != typeof(bool))
568+
{
569+
continue;
570+
}
571+
572+
var tryParseParameters = method.GetParameters();
573+
574+
if (tryParseParameters.Length == 2 &&
575+
tryParseParameters[0].ParameterType == typeof(string) &&
576+
tryParseParameters[1].IsOut &&
577+
tryParseParameters[1].ParameterType == type.MakeByRefType())
578+
{
579+
return method;
580+
}
579581
}
580-
}
581582

582-
return null;
583-
}
583+
return null;
584+
}
584585

585-
private static MethodInfo? FindTryParseMethod(Type type)
586-
{
587-
return TryParseMethodCache.GetOrAdd(type, FindTryParseMethodUncached);
586+
return TryParseMethodCache.GetOrAdd(type, Finder);
588587
}
589588

590589
private static bool HasTryParseMethod(ParameterInfo parameter)
@@ -621,7 +620,6 @@ private static Expression BindParameterFromValue(ParameterInfo parameter, Expres
621620
// string tempSourceString;
622621
//
623622
// // Assume "[FromRoute] int id" is the first parameter.
624-
//
625623
// tempSourceString = httpContext.RequestValue["id"];
626624
//
627625
// int param1 = tempSourceString == null ? default :
@@ -666,7 +664,7 @@ private static Expression BindParameterFromValue(ParameterInfo parameter, Expres
666664
Expression.Constant(parameter.DefaultValue) :
667665
Expression.Default(parameter.ParameterType);
668666

669-
// tempSourceString = httpContext.RequestValue["id];
667+
// tempSourceString = httpContext.RequestValue["id"];
670668
var storeValueToTemp = Expression.Assign(TempSourceStringExpr, valueExpression);
671669

672670
// int param1 = tempSourcString == null ? default : ...

src/Http/Http.Extensions/test/RequestDelegateFactoryTests.cs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -307,60 +307,37 @@ void Store<T>(HttpContext httpContext, T tryParsable)
307307

308308
return new[]
309309
{
310-
// String (technically not "TryParsable", but it's the the special case)
310+
// string is not technically "TryParsable", but it's the special case.
311311
new object[] { (Action<HttpContext, string>)Store, "plain string", "plain string" },
312-
// Int32
313312
new object[] { (Action<HttpContext, int>)Store, "-42", -42 },
314313
new object[] { (Action<HttpContext, uint>)Store, "42", 42U },
315-
// Byte
316314
new object[] { (Action<HttpContext, bool>)Store, "true", true },
317-
// Int16
318315
new object[] { (Action<HttpContext, short>)Store, "-42", (short)-42 },
319316
new object[] { (Action<HttpContext, ushort>)Store, "42", (ushort)42 },
320-
// Int64
321317
new object[] { (Action<HttpContext, long>)Store, "-42", -42L },
322318
new object[] { (Action<HttpContext, ulong>)Store, "42", 42UL },
323-
// IntPtr
324319
new object[] { (Action<HttpContext, IntPtr>)Store, "-42", new IntPtr(-42) },
325-
// Char
326320
new object[] { (Action<HttpContext, char>)Store, "A", 'A' },
327-
// Double
328321
new object[] { (Action<HttpContext, double>)Store, "0.5", 0.5 },
329-
// Single
330322
new object[] { (Action<HttpContext, float>)Store, "0.5", 0.5f },
331-
// Half
332323
new object[] { (Action<HttpContext, Half>)Store, "0.5", (Half)0.5f },
333-
// Decimal
334324
new object[] { (Action<HttpContext, decimal>)Store, "0.5", 0.5m },
335-
// DateTime
336325
new object[] { (Action<HttpContext, DateTime>)Store, now.ToString("o"), now },
337-
// DateTimeOffset
338326
new object[] { (Action<HttpContext, DateTimeOffset>)Store, nowOffset.ToString("o"), nowOffset },
339-
// TimeSpan
340327
new object[] { (Action<HttpContext, TimeSpan>)Store, TimeSpan.FromSeconds(42).ToString(), TimeSpan.FromSeconds(42) },
341-
// Guid
342328
new object[] { (Action<HttpContext, Guid>)Store, guid.ToString(), guid },
343-
// Version
344329
new object[] { (Action<HttpContext, Version>)Store, "6.0.0.42", new Version("6.0.0.42") },
345-
// BigInteger
346330
new object[] { (Action<HttpContext, BigInteger>)Store, "-42", new BigInteger(-42) },
347-
// IPAddress
348331
new object[] { (Action<HttpContext, IPAddress>)Store, "127.0.0.1", IPAddress.Loopback },
349-
// IPEndPoint
350332
new object[] { (Action<HttpContext, IPEndPoint>)Store, "127.0.0.1:80", new IPEndPoint(IPAddress.Loopback, 80) },
351-
// System Enums
352333
new object[] { (Action<HttpContext, AddressFamily>)Store, "Unix", AddressFamily.Unix },
353334
new object[] { (Action<HttpContext, ILOpCode>)Store, "Nop", ILOpCode.Nop },
354335
new object[] { (Action<HttpContext, AssemblyFlags>)Store, "PublicKey,Retargetable", AssemblyFlags.PublicKey | AssemblyFlags.Retargetable },
355-
// Nullable<T>
356336
new object[] { (Action<HttpContext, int?>)Store, "42", 42 },
357-
// Null route and/or query value gives default value.
358-
new object?[] { (Action<HttpContext, int>)Store, null, 0 },
359-
new object?[] { (Action<HttpContext, int?>)Store, null, null },
360-
// Custom Enum
361337
new object[] { (Action<HttpContext, MyEnum>)Store, "ValueB", MyEnum.ValueB },
362-
// Custom record with static TryParse method!
363338
new object[] { (Action<HttpContext, MyTryParsableRecord>)Store, "42", new MyTryParsableRecord(42) },
339+
new object?[] { (Action<HttpContext, int>)Store, null, 0 },
340+
new object?[] { (Action<HttpContext, int?>)Store, null, null },
364341
};
365342
}
366343
}

0 commit comments

Comments
 (0)