diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
index 8864306..53bd595 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
@@ -239,6 +239,50 @@ private static BinaryExpression ApplyNullCoalescing(Identifier op, Expression lh
return null;
}
+ ///
+ /// Creates a string.Concat call for the + operator when at least one of its operands is a string.
+ ///
+ /// The expression of the left operand.
+ /// The expression of the right operand.
+ ///
+ /// The expression that calls string.Concat.
+ ///
+ ///
+ /// A string.Concat call among the operands, such as the one built for a + b, is replaced with its
+ /// arguments, so a + b + c becomes a single call. For a call in the expanded form, these are the elements
+ /// of its array; a call with a single argument of another kind, such as string.Concat(array), stays one
+ /// operand. If any operand is not a string, all operands are passed as .
+ ///
+ private static MethodCallExpression ApplyStringConcatenation(Expression lhs, Expression rhs)
+ {
+ List arguments = [.. GetConcatOperands(lhs), .. GetConcatOperands(rhs)];
+
+ var type = arguments.TrueForAll(a => a.Type == typeof(string)) ? typeof(string) : typeof(object);
+ var types = new Type[arguments.Count];
+ Array.Fill(types, type);
+
+ var method = (MethodInfo?)MethodResolver.ResolveMethod(s_concatMethods, types);
+ Debug.Assert(method is not null);
+
+ return Expression.Call(method, CreateInjectingArguments(method, arguments));
+
+ static IReadOnlyList GetConcatOperands(Expression expression)
+ {
+ if (expression is MethodCallExpression call
+ && call.Method.Name == nameof(string.Concat)
+ && call.Method.DeclaringType == typeof(string))
+ {
+ if (call.Arguments.Count != 1)
+ return call.Arguments;
+
+ if (call.Arguments[0] is NewArrayExpression { NodeType: ExpressionType.NewArrayInit } array)
+ return array.Expressions;
+ }
+
+ return [expression];
+ }
+ }
+
///
/// Determines whether the specified expression is the null literal, possibly in parentheses.
///
diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
index c569607..c70982c 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
@@ -87,69 +87,8 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
case "??":
return ApplyNullCoalescing(expr.Operator, lhs, rhs);
- case "+":
- if (lhsType != typeof(string) && rhsType != typeof(string))
- break;
-
- var arguments = new List();
- var argumentTypes = new List();
-
- Flatten(lhs);
- Flatten(rhs);
-
- if (argumentTypes.Any(t => t != typeof(string)))
- {
- for (var i = 0; i < arguments.Count; i++)
- {
- argumentTypes[i] = typeof(object);
- if (arguments[i].Type.IsValueType)
- arguments[i] = Expression.Convert(arguments[i], typeof(object));
- }
- }
-
- var cmi = (MethodInfo?)MethodResolver.ResolveMethod(s_concatMethods, argumentTypes.ToArray())!;
- Debug.Assert(cmi is not null);
-
- var args = CreateInjectingArguments(cmi, arguments);
- return Expression.Call(null, cmi, args);
-
- void Flatten(Expression e)
- {
- if (e is MethodCallExpression call
- && call.Method.Name == nameof(string.Concat)
- && call.Method.DeclaringType == typeof(string))
- {
- var list = call.Arguments;
- if (list.Count != 1)
- {
- foreach (var item in list)
- {
- arguments.Add(item);
- argumentTypes.Add(item.Type);
- }
- }
- else if (list[0] is NewArrayExpression { NodeType: ExpressionType.NewArrayInit } array)
- {
- 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
- {
- arguments.Add(e);
- argumentTypes.Add(e.Type);
- }
- }
+ case "+" when lhsType == typeof(string) || rhsType == typeof(string):
+ return ApplyStringConcatenation(lhs, rhs);
}
try