zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
459 stars 86 forks source link

Can not detect type #120

Closed cuiliang closed 2 years ago

cuiliang commented 2 years ago

Sample code: https://dotnetfiddle.net/Q2Zh4s

Code to Execute():

var code = new[] { "a" };
foreach (var line in code)
{
    var a = new List<string>() { line };
}

Error:

System.InvalidOperationException: No method 'Add' on type 'System.Collections.Generic.List`1[System.String]' is compatible with the supplied arguments.
   at System.Linq.Expressions.Expression.FindMethod(Type type, String methodName, Type[] typeArgs, Expression[] args, BindingFlags flags)
   at System.Linq.Expressions.Expression.ListInit(NewExpression newExpression, IEnumerable`1 initializers)
   at .(ExpressionScope , SyntaxNode )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , ExpressionScope , String , Boolean , Boolean )
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , Boolean , Boolean , Boolean )
   at Z.Expressions.EvalContext.Execute[TResult](String code)
   at Z.Expressions.EvalContext.Execute(String code)
   at Z.Expressions.Eval.Execute(String code)
   at Program.Main()

To solve the problem, need to give direct type to iterator variable:

var code = new[] { "a" };
foreach (string line in code)  <--- change var to string
{
    var a = new List<string>() { line };
}
JonathanMagnan commented 2 years ago

Hello @cuiliang ,

The fix has been merged.

It should be released tomorrow (Wednesday)

Best Regards,

Jon

cuiliang commented 2 years ago

Thank you @JonathanMagnan !

JonathanMagnan commented 2 years ago

Hello @cuiliang ,

The v4.0.68 has been released.

Let me know if everything is working in this new version.

Best Regards,

Jon