dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
1.99k stars 374 forks source link

InvalidOperationException when comparing two nullable numeric values #276

Closed Hakim83 closed 1 year ago

Hakim83 commented 1 year ago

The following code was evaluating well till version 2.11.0

interpreter.Eval<bool>("((int?)5)>((double?)4)");

later versions (including current version 2.13.0), throws the following exception:

System.InvalidOperationException : The binary operator GreaterThan is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Nullable`1[System.Double]'

tested in .NET6

metoule commented 1 year ago

This was a side effect of #231,

The fix is to perform a better check to see if a method is applicable (by better matching generic types).

Hakim83 commented 1 year ago

Ok, in fact I got the same exception when running non-nullable different types but using null conditional operator, e.g. class Foo { public int var1; public double var2; } then, following throws that exception: interpreter.Eval("foo?.var1>foo?.var2")

metoule commented 1 year ago

That's the same error, since the null conditional operator is just syntactic sugar :)

int? var1 = null;
if (foo != null)
  var1 = foor.var1;

double? var2 = null;
if (foo != null)
  var2 = foor.var2;

return var1 > var2;