From 6cd743137446c4f73eaabb8c59c9f8c069d16520 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 00:06:48 +0500 Subject: [PATCH 1/2] parser: keep a single-argument string.Concat call as one operand When the '+' operator merged the operands of a nested string.Concat call, it took the only argument of the call for the array created for the expanded form. Another overload with one argument failed with InvalidCastException: for example, 'string.Concat("x") + "y"' calls Concat(object). Merge only the elements of an array created for the expanded form, and keep other single-argument calls as one operand. --- src/Ramstack.ExpressionParser/ExpressionBuilder.cs | 12 ++++++++++-- .../Data/StringConcat.txt | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 4f82ec5..972ddfc 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -130,15 +130,23 @@ void Flatten(Expression e) argumentTypes.Add(item.Type); } } - else + else if (list[0] is NewArrayExpression { NodeType: ExpressionType.NewArrayInit } array) { - var array = (NewArrayExpression)list[0]; foreach (var item in array.Expressions) { arguments.Add(item); argumentTypes.Add(item.Type); } } + else + { + // + // The only argument is not an array of operands created for the expanded form, + // as in string.Concat(value) or string.Concat(array), so the call itself is the operand. + // + arguments.Add(e); + argumentTypes.Add(e.Type); + } } else { diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/StringConcat.txt b/tests/Ramstack.ExpressionParser.Tests/Data/StringConcat.txt index 59ef257..1ad15e7 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/StringConcat.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/StringConcat.txt @@ -30,3 +30,6 @@ Concat(new [] {"1", Convert(2, Object), "3", Convert(4, Object), Convert(5, Obje "1" + 2 + "3" + (4 + 5) + 6 Concat(new [] {"1", Convert(2, Object), "3", Convert((4 + 5), Object), Convert(6, Object)}) + +string.Concat("x") + "y" +Concat(Concat("x"), "y") From b40104c5915843867a0b2fd1979406dcf6ce0d47 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 18 Nov 2026 00:06:55 +0500 Subject: [PATCH 2/2] parser: pass an array argument as the parameter array The arguments for a params parameter were always wrapped in a new array, even when the call was resolved in its normal form with an array passed directly. As a result, 'string.Join(",", words)' with a string[] failed with "No coercion operator is defined between types 'System.String[]' and 'System.String'", and a string[] passed to 'params object[]' became the only element of a new array. Pass the only argument for the parameter array as the array itself when it converts to the array type, as C# does. In other cases the arguments are still wrapped in an array. --- .../ExpressionBuilder.Helpers.cs | 27 +++++++++++------- .../ExpressionParserTest.cs | 28 +++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs index 12e2b74..2243242 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs @@ -160,19 +160,26 @@ private static List CreateInjectingArguments(MethodInfo method, IRea Debug.Assert(parameter.ParameterType.IsSZArray); Debug.Assert(parameter.ParameterType.GetElementType() is not null); - var paramArray = new List(); - var paramArrayType = parameter.ParameterType.GetElementType()!; + // + // When the only argument for the parameter array converts to the array type, + // the call is in its normal form, and the argument is passed as the array itself, as in C#. + // MethodResolver selects the normal form under the same condition. + // + if (i != args.Count - 1 || !parameter.ParameterType.IsAssignableFrom(arg.Type)) + { + var paramArray = new List(); + var paramArrayType = parameter.ParameterType.GetElementType()!; - for (; i < args.Count; i++) - paramArray.Add( - Convert(args[i], paramArrayType)); + for (; i < args.Count; i++) + paramArray.Add(Convert(args[i], paramArrayType)); - list.Add( - Expression.NewArrayInit( - paramArrayType, - paramArray)); + list.Add( + Expression.NewArrayInit( + paramArrayType, + paramArray)); - break; + break; + } } list.Add(Convert(arg, parameter.ParameterType)); diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 8cf86ff..ef72055 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -133,6 +133,23 @@ public void EvaluateExpression(string expression, object result) => public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) => Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf().And.EqualTo(expected)); + [TestCase("Sum(Numbers)", 6)] + [TestCase("Count(Words)", 2)] + [TestCase("IsWords(Words)", true)] + [TestCase("""string.Join(",", Words)""", "a,b")] + [TestCase("string.Concat(Objects)", "x1")] + [TestCase("""string.Concat(Objects) + "!" """, "x1!")] + public void Evaluate_ArrayForParamArray_PassesArray(string expression, object expected) => + Assert.That(ExpressionParser.Evaluate(expression, new ParamArrayContext()), Is.EqualTo(expected)); + + [TestCase("Sum(1, 2, 3)", 6)] + [TestCase("Sum(5)", 5)] + [TestCase("Count(Numbers)", 1)] + [TestCase("Count(Words, Words)", 2)] + [TestCase("""string.Join(",", "a", "b")""", "a,b")] + public void Evaluate_ElementsForParamArray_CreatesArray(string expression, object expected) => + Assert.That(ExpressionParser.Evaluate(expression, new ParamArrayContext()), Is.EqualTo(expected)); + [TestCase("!Flag", false)] [TestCase("!NullableFlag", true)] [TestCase("!WithLogicalNot", true)] @@ -356,4 +373,15 @@ public class OverloadContext public static string Pick(int _) => "int"; public static string OnlyChar(char _) => "char"; } + + public class ParamArrayContext + { + public string[] Words { get; } = ["a", "b"]; + public int[] Numbers { get; } = [1, 2, 3]; + public object[] Objects { get; } = ["x", 1]; + + public static int Sum(params int[] values) => values.Sum(); + public static int Count(params object[] values) => values.Length; + public bool IsWords(params string[] values) => ReferenceEquals(values, Words); + } }