Flee icon indicating copy to clipboard operation
Flee copied to clipboard

Support Expressions that call Extension Methods.

Open moh-hassan opened this issue 6 years ago • 3 comments

I tried to evalauate expression that use extension method

         var context = new ExpressionContext();
        context.Imports.AddType(typeof(Extensions)); //Extension methods class 
        context.Variables["s"] = "this is a string";
        //expresion use extension method Quote()
        var expression = "s.Quote()"; //raise error: ExpressionCompileException
        //var expression = "Quote(\"abc\")"; //this is working
        var e = context.CompileDynamic(expression);
        var result = e.Evaluate();
        Console.WriteLine(result);
        
		
		//Extension methods  class 
	  public static class Extensions
		{
			public static string Quote(this string text)
			{
				return $"\"{text}\"";
			}
		}

I get Error Exception:

Flee.PublicTypes.ExpressionCompileException : FunctionCallElement: Could find not function 'Quote()' on type 'String'

Support Extension methods or, what i missed to use extension methods?

moh-hassan avatar Jan 15 '19 18:01 moh-hassan

This would require a way to register the assemblies containing your extension methods.

That's a lot of work to accomplish what can basically be done with the Expression Owner convention—the syntax is just a method call instead of an extension method invocation.

piranout avatar Jan 15 '19 18:01 piranout

Thanks for reply.

The context already Import any type with its qualified name (assembly.type)

         context.Imports.AddType(typeof(Extensions)); //static methods class 

Currently, I can evaluate the expression as a function call to the static methods(which is really an extension method). In our example:

              //this function is extension method but used as a function call 
             var expression = "Quote(\"abc\")"; //it's working

So all public static methods are feasible to the expression engine. Extension methods is valuable part of DotNet.

moh-hassan avatar Jan 15 '19 18:01 moh-hassan

I had that same issue some time ago and solved it in a fork of a vb version.

Now I have proted that to C# and created PR #62

ThomasZitzler avatar Apr 11 '19 10:04 ThomasZitzler