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

ExpandoObject Expression / Brackets - NullReferenceException #68

Closed michaelsudnik closed 4 years ago

michaelsudnik commented 4 years ago

When I try to evaluate the following expression by calling Eval.Execute

-(Param1 - Param2.Property1)

Where Param1 is an Integer and Param2 is an ExpandoObject (Property1 is not defined), I get a NullReferenceException (I think it should report a RuntimeBinderException instead).

Z.Expressions.Eval.dll!Z.Expressions.CodeCompiler.CSharp.ExpressionScope.CreateBody(bool isMain, System.Type tResult)   Unknown
Z.Expressions.Eval.dll!Z.Expressions.CodeCompiler.CSharp.ExpressionParser.ParseSyntax(Z.Expressions.CodeCompiler.CSharp.ExpressionScope scope, Z.Expressions.CodeAnalysis.SyntaxNode node, System.Type resultType)  Unknown
Z.Expressions.Eval.dll!.<System.Func<System.Collections.IDictionary, object>>(Z.Expressions.EvalContext , string , System.Collections.Generic.IDictionary<string, System.Type> , System.Type , Z.Expressions.EvalCompilerParameterKind , Z.Expressions.CodeCompiler.CSharp.ExpressionScope , string , bool , bool )  Unknown
Z.Expressions.Eval.dll!.<System.Func<System.Collections.IDictionary, object>>(Z.Expressions.EvalContext , string , System.Collections.Generic.IDictionary<string, System.Type> , System.Type , Z.Expressions.EvalCompilerParameterKind , bool , bool ) Unknown
Z.Expressions.Eval.dll!Z.Expressions.EvalContext.Execute<object>(string code, object parameters)    Unknown
Z.Expressions.Eval.dll!Z.Expressions.Eval.Execute(string code, object parameters)   Unknown

E.g, if I evaluate the following expression

Param1 - Param2.Property1

I correctly get a RuntimeBinderException

{"'System.Dynamic.ExpandoObject' does not contain a definition for 'Property1'"}

michaelsudnik commented 4 years ago

https://dotnetfiddle.net/TxHMh5

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

Thank you for reporting, we will look at it and try to report the right error if that's possible.

Best Regards,

Jon

michaelsudnik commented 4 years ago

Many thanks for looking into it. It would be excellent if you could find a solution to this.

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

Just to let you know that it will take a few days to fix it.

While doing this request, my developer found some cases that we should be able to support with the expando (such as in a dictionary).

He believes he will be able to complete his test on Friday, so we will probably be able to release the fix early next week.

michaelsudnik commented 4 years ago

Hi @JonathanMagnan, this is actually a little bit more serious as it is not just a problem when the dynamic property is missing, but also throws the NullReferenceException when it is present as well. It seems to just be a problem with the minus sign in front of the bracket. How close to a fix are you?

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

A version has been released this morning.

I did some tests with your initial case, on my side, I no longer get the problem. If that's not the case for you, please provide us a full example since it might depend on how you pass the Param1 and Param2.

My developer asked me to not close the issue and re-assign it to him. I believe he wants to fix more cases, I will discuss with him next Monday.

Let me know if everything is fixed for you or there is still some problem.

michaelsudnik commented 4 years ago

Hi - thanks it does seem better.

Here is another failure scenario though: https://dotnetfiddle.net/15cAsM

(Property2 does not exist)

michaelsudnik commented 4 years ago

Any update on this?

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

We are currently testing the fix, we believe it will be deployed today or tomorrow.

Our library now throw the right error:

System.Dynamic.ExpandoObject' does not contain a definition for 'Property2

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

A new version is now available.

The right error message should be now raised as the code now allow to negate value of type object (in your case the property that doesn't exist yet).

Let me know if that work as expected.

michaelsudnik commented 4 years ago

That seems to be working as expected now. Thanks for your help!

JonathanMagnan commented 4 years ago

Awesome @michaelsudnik

we are glad everything is working out for you!

feel free to contact us for questions, issues or feedback.

best regards,

Jon