Skip to content

Commit 0fc8f4b

Browse files
authored
Use Array.Empty for parameterless filters (#41740)
1 parent 195f371 commit 0fc8f4b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static partial class RequestDelegateFactory
5656
private static readonly MethodInfo ValueTaskOfTToValueTaskOfObjectMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ValueTaskOfTToValueTaskOfObject), BindingFlags.NonPublic | BindingFlags.Static)!;
5757
private static readonly MethodInfo PopulateMetadataForParameterMethod = typeof(RequestDelegateFactory).GetMethod(nameof(PopulateMetadataForParameter), BindingFlags.NonPublic | BindingFlags.Static)!;
5858
private static readonly MethodInfo PopulateMetadataForEndpointMethod = typeof(RequestDelegateFactory).GetMethod(nameof(PopulateMetadataForEndpoint), BindingFlags.NonPublic | BindingFlags.Static)!;
59+
private static readonly MethodInfo ArrayEmptyMethod = typeof(Array).GetMethod(nameof(Array.Empty), BindingFlags.Public | BindingFlags.Static)!;
5960

6061
// Call WriteAsJsonAsync<object?>() to serialize the runtime return type rather than the declared return type.
6162
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
@@ -388,9 +389,12 @@ private static Expression CreateRouteHandlerInvocationContextBase(FactoryContext
388389
// In the event that a constructor matching the arity of the
389390
// provided parameters is not found, we fall back to using the
390391
// non-generic implementation of RouteHandlerInvocationContext.
392+
Expression paramArray = factoryContext.BoxedArgs.Length > 0
393+
? Expression.NewArrayInit(typeof(object), factoryContext.BoxedArgs)
394+
: Expression.Call(ArrayEmptyMethod.MakeGenericMethod(new Type[] { typeof(object) }));
391395
var fallbackConstruction = Expression.New(
392396
DefaultRouteHandlerInvocationContextConstructor,
393-
new Expression[] { HttpContextExpr, Expression.NewArrayInit(typeof(object), factoryContext.BoxedArgs) });
397+
new Expression[] { HttpContextExpr, paramArray });
394398

395399
var arguments = new Expression[factoryContext.ArgumentExpressions.Length + 1];
396400
arguments[0] = HttpContextExpr;

0 commit comments

Comments
 (0)