This project has moved and is read-only. For the latest updates, please go here.

ScopeCompile() expecting the types already present

Jun 29, 2016 at 10:17 AM
Edited Jun 29, 2016 at 10:19 AM
Hi,
First of all thanks for building this fantastic library for evaluating the dynamic expressions. It has helped us a lot.
Till now I was using the older version (1.0.4) of the assembly and recently tried upgrading it to the latest version.
I thought of just replacing the old assembly with new one and test the application.
But when I ran the application I got below error messages while calling the method ScopeCompile().
 Dictionary<string, Func<object, object>> ruleCache = new Dictionary<string, Func<object, object>>();
            var execRule = new CompiledExpression() { StringToParse = "(!helper.IsCorrect(obj.ProductId,\"1234\"))" };
            Func<object, object> preCompiledRule = execRule.ScopeCompile();
            ruleCache.Add("1", preCompiledRule);
An unhandled exception of type 'ExpressionEvaluator.Parser.ExpressionParseException' occurred in ExpressionEvaluator.dll

Additional information: Cannot resolve symbol "helper" at line 1 char 2
To make it work I modified the code as:
 class Helper
        {
            public bool IsCorrect(int pid, string value)
            {
                return false;
            }
        }

        class MyDynamicClass : DynamicObject
        {

        }
static void Main(string[] args)
        {
Dictionary<string, Func<object, object>> ruleCache = new Dictionary<string, Func<object, object>>();
            var t = new TypeRegistry();
            t.RegisterType("helper", typeof(Helper));
            t.RegisterType("obj", typeof(MyDynamicClass));
            var execRule = new CompiledExpression() { StringToParse = "(!helper.IsCorrect(obj.ProductId,\"1234\"))" };
            execRule.TypeRegistry = t;
            Func<object, object> preCompiledRule = execRule.ScopeCompile();
            ruleCache.Add("1", preCompiledRule);
}
This again gave me error of missing property:
An unhandled exception of type 'ExpressionEvaluator.Parser.ExpressionParseException' occurred in ExpressionEvaluator.dll

Additional information: Cannot resolve member "ProductId" on type "RuntimeType" at line 1 char 23
Now as you can see the object "obj" wont have static properties but have dynamic properties added to it at runtime. And during ScopeCompile this wont be available to me. If at all it is available there can be thousands of properties that can be part of the class MyDynamicClass. So is there anyway to ScopeCompile() the expression without registering the types.

In the earlier version of assembly that I am using (1.0.4) the very first block of code worked perfectly fine without complaining about any missing type. It resolved and scope compiled the expression. But in this new version its failing.

The reason why I am scopecompiling it is because I have around 5000 expressions and I need to precompile them a cache them so that the next calls are quick.

Thanks.
Sep 16, 2016 at 12:41 AM
The way the object and dynamic type is handled was changed in 2.x. Since dynamic is basically an object type, it made it difficult to distinguish between the two. Dynamic types should still be supported in 2.x, but I'll need to look at what's happening.

You might want to try Roslyn's scripting API. It also features a something like ScopeCompile. and might support dynamics better.

https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples