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

Is it possible to support using an anonymous method to declare and initialize a delegate? #92

Closed cesaryuan closed 3 years ago

cesaryuan commented 3 years ago

Is it possible to support using an anonymous method or a lambda expression to declare and initialize a delegate? I test the follow example, but it throw error.

var context = new EvalContext();
context.RegisterType(typeof(Regex));
context.RegisterType(typeof(Match));
var result = context.Execute(@"
    Regex.Replace(""test"", @""\b[a-z]\w+"", (Match match) =>
    {
        return """";
    });
");

The detail errors like this,

Z.Expressions.Compiler.Shared.EvalException
  HResult=0x80131500
  Message=Oops! No applicable member has been found for the expression. The error occurred for expression "." at position 23 near ".Replace(\"test\", @\"\b[a-z".
  Source=Z.Expressions.Eval
  StackTrace:
   at .(ExpressionScope , Expression , Type , String , List`1 , Boolean , List`1 , SyntaxNode , Expression& )
   at .(ExpressionScope , SyntaxNode , Type , Expression , List`1 )
   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 SomeTest.Program.Main(String[] args) in E:\Desktop\Program\C#\SomeTest\Program.cs:line 41

  此异常最初是在此调用堆栈中引发的: 
    [外部代码]
    SomeTest.Program.Main(string[]) (位于 Program.cs 中)
JonathanMagnan commented 3 years ago

Hello @cesaryuan ,

Thank you for reporting, we will look at this to see if that's something we could support or not.

Best Regards,

Jon

JonathanMagnan commented 3 years ago

Hello @se ,

The v4.0.29 has been released.

Could you try it and let us know if we successfully added the code to handle delegate correctly?

Best Regards,

Jon

cesaryuan commented 3 years ago

Works for me! But it seems that still not support using delegate keyword to initialize an delegate (you said in the readme that you support all keyword). Anyway, lambda expression is enough. Thank you! @JonathanMagnan

JonathanMagnan commented 3 years ago

Thank,

We will look at it if we can do it.

JonathanMagnan commented 3 years ago

Hello @cesaryuan ,

It took us a while but the v4.0.30 has been finally released.

We now support several case with delegate such as:

var delegateAction = Eval.Execute<Action>(@"Action greet = delegate { Console.WriteLine(""Hello!""); };");
delegateAction();

var delegateFunc = Eval.Execute<Func<int, int, int>>(@"Func<int, int, int> sum = delegate (int a, int b) { return a + b; }; return sum;");
var x1 = delegateFunc(1, 2);

If you still have some issue with your delegate, please provide us a full example about what you will want to do and we will look at it.

Best Regards,

Jon

cesaryuan commented 3 years ago

@JonathanMagnan Thanks for your effort! This is genuinely nice, and there is no issue for me.