Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String concatenations fails with System.ArgumentException during parsing #845

Closed
victorr99 opened this issue Oct 14, 2024 · 4 comments
Closed
Assignees
Labels

Comments

@victorr99
Copy link

1. Description

An expression like "foo" & SomeVar fails when SomeVar is not a reference type. When Parse() is called a System.ArgumentException is raised stating that int cannot be used for object arguments. I assume the value needs to be manually boxed before being passed to string.Concat. If I create an expression manually I get the same error. When I firstly convert the value to object, the Concat works just fine.

2. Exception

Unhandled exception. System.ArgumentException: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'System.String Concat(System.Object, System.Object)' (Parameter 'arg1')
   at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0, Expression arg1)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
   at System.Linq.Dynamic.Core.Parser.ExpressionHelper.GenerateStaticMethodCall(String methodName, Expression left, Expression right)
   at System.Linq.Dynamic.Core.Parser.ExpressionHelper.GenerateStringConcat(Expression left, Expression right)
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator()
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor)
   at TypeBuilderWithExpression.Program.Main(String[] args) 

3. Fiddle or Project

If you are able,

Provide a Fiddle that reproduce the issue: https://dotnetfiddle.net/25Vjsn

var parser = new ExpressionParser(
    [Expression.Parameter(typeof(int), "VarA")],
    "\"foo\" & VarA",
    [],
    null);
        
var expression = parser.Parse(typeof(string));

4. Any further technical details

This doesn't work:

var method = typeof(string)
    .GetMethods(BindingFlags.Public | BindingFlags.Static)
    .Where(m => m.Name == "Concat" && m.GetParameters().Select(p => p.ParameterType).SequenceEqual([typeof(object[])]))
    .First();

var param = Expression.Parameter(typeof(int));
var expression = Expression.Call(null, method, Expression.NewArrayInit(typeof(object), Expression.Constant("Hello"), param));

var callable = Expression.Lambda<Func<int, string>>(expression, param).Compile();
var r = callable.Invoke(20);

Console.WriteLine(r);

But this does:

var method = typeof(string)
    .GetMethods(BindingFlags.Public | BindingFlags.Static)
    .Where(m => m.Name == "Concat" && m.GetParameters().Select(p => p.ParameterType).SequenceEqual([typeof(object[])]))
    .First();

var param = Expression.Parameter(typeof(int));
var boxed = Expression.Convert(param, typeof(object));
var expression = Expression.Call(null, method, Expression.NewArrayInit(typeof(object), Expression.Constant("Hello"), boxed));

var callable = Expression.Lambda<Func<int, string>>(expression, param).Compile();
var r = callable.Invoke(20);

Console.WriteLine(r);
@StefH StefH self-assigned this Oct 15, 2024
@StefH
Copy link
Collaborator

StefH commented Oct 15, 2024

@victorr99
The dotnet fiddle links to the wrong example, can you please fix this?

@StefH StefH added the bug label Oct 15, 2024
@StefH
Copy link
Collaborator

StefH commented Oct 15, 2024

#847

@StefH
Copy link
Collaborator

StefH commented Oct 15, 2024

fixed

@StefH StefH closed this as completed Oct 15, 2024
@victorr99
Copy link
Author

Thanks for the quick fix. Any idea when the fix will be released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants