zzzprojects / Eval-Expression.NET

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

Exception: The number of generic arguments provided doesn't equal the arity of the generic type definition. #121

Closed cuiliang closed 2 years ago

cuiliang commented 2 years ago

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

Problem only happend if add this line:

EvalManager.DefaultContext.RegisterAutoAddMissingTypeAssembly(typeof(System.Data.DataRowCollection).Assembly); 

Full code:

using System;
using System.Drawing;
using Z.Expressions;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");   

        EvalManager.DefaultContext.RegisterAutoAddMissingTypeAssembly(typeof(System.Drawing.Bitmap).Assembly);
                // The following line will cause problem.
        EvalManager.DefaultContext.RegisterAutoAddMissingTypeAssembly(typeof(System.Data.DataRowCollection).Assembly);  
        EvalManager.DefaultContext.AutoAddMissingTypes = true;

        string code = @"

        Func<Bitmap, string> GetFormat = (Bitmap bitmap) =>
            {
               return ""dddd"";
            };

        return GetFormat(null); 

        ";

        var rtn = Eval.Execute(code);
        Console.WriteLine(rtn);
    }
}

Exception:

Hello World
Run-time exception (line 29): The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation

Stack Trace:

[System.ArgumentException: The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation]
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at .(ExpressionScope , SyntaxNode , StringBuilder , Int32& , Boolean )
   at .(ExpressionScope , SyntaxNode , Boolean )
   at .(ExpressionScope , SyntaxNode )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at Z.Expressions.CodeCompiler.CSharp.ExpressionParser.ParseSyntax(ExpressionScope scope, SyntaxNode node, Type resultType)
   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.Eval.Execute(String code)
   at Program.Main() :line 29
JonathanMagnan commented 2 years ago

Hello @cuiliang ,

The problem happen because the DataRowCollection have also a type func which currently overrides the System.Func you want to use.

System.Data.Listeners`1+Func`2

We are currently looking at it to remove this conflict, so the System.Func always win

Best Regards,

Jon

JonathanMagnan commented 2 years ago

Hello @cuiliang ,

The v4.0.70 has been released.

This issue should now be fixed.

Best Regards,

Jon

cuiliang commented 2 years ago

@JonathanMagnan Thank you ~