From c16181a1814a9770ded157a5dbdcacc5bc919b0f Mon Sep 17 00:00:00 2001 From: Dominique Schuppli Date: Sun, 8 Feb 2026 21:15:22 +0100 Subject: [PATCH] Don't allocate empty `IInvocation.Arguments` arrays --- .../MethodWithInvocationGenerator.cs | 3 ++- .../DynamicProxy/Tokens/ArrayMethods.cs | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/Castle.Core/DynamicProxy/Tokens/ArrayMethods.cs diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs index 8c475586c..9ec97f9f6 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs @@ -266,7 +266,8 @@ public void CopyIn(out LocalReference argumentsArray, out bool hasByRefArguments method.CodeBuilder.AddStatement( new AssignStatement( argumentsArray, - new NewArrayExpression(arguments.Length, typeof(object)))); + arguments.Length > 0 ? new NewArrayExpression(arguments.Length, typeof(object)) + : new MethodInvocationExpression(instance: null, ArrayMethods.EmptyOfObject))); for (int i = 0, n = arguments.Length; i < n; ++i) { diff --git a/src/Castle.Core/DynamicProxy/Tokens/ArrayMethods.cs b/src/Castle.Core/DynamicProxy/Tokens/ArrayMethods.cs new file mode 100644 index 000000000..1521a6ad4 --- /dev/null +++ b/src/Castle.Core/DynamicProxy/Tokens/ArrayMethods.cs @@ -0,0 +1,24 @@ +// Copyright 2004-2026 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.DynamicProxy.Tokens +{ + using System; + using System.Reflection; + + internal static class ArrayMethods + { + public static readonly MethodInfo EmptyOfObject = typeof(Array).GetMethod(nameof(Array.Empty)).MakeGenericMethod(typeof(object)); + } +}