-
-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exception when using .? operator for nullable fields #169
Comments
Expression expression2 = this.ParseMemberAccess((Type) null, expression1);
if (expression2.Type.IsValueType)
expression2 = Parser.PromoteExpression(expression2, typeof (Nullable<>).MakeGenericType(expression2.Type), true); This check - "expression2.Type.IsValueType" will return true for int? and an additional check like "Nullable.GetUnderlyingType(expression2.Type) is null" should be added to if statement. |
…on that ensures a nullable type isn't converted to a nullable of nullable. Fixes dynamicexpresso#169
Oh I didn't see your pull request. There's actually an existing method that does the check already: |
@davideicardi @metoule Found one more unclear behaviour new Interpreter().SetVariable("x", (int?)null).Eval("x?.ToString()"); This will throw DynamicExpresso.Exceptions.ParseException: Invalid Operation (at index 13).
---> System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Object'. |
That exception occurs because of the internal conversion that happens under the hood:
is converted to: int? x;
object nullConstant = null;
if (x == nullConstant) { // invalid operation
return nullConstant;
} else {
return x.ToString();
} but a nullable integer can't be compared to an Fortunately, it's possible to use the |
Thanks @halamah and @metoule for bug report and fix! New version is on the way ... |
* ?. and ?[ operators now use the existing GenerateNullableTypeConversion that ensures a nullable type isn't converted to a nullable of nullable. Fixes #169 * Promote operands before generating the equality check on ?. and ?[ operators.
* ?. and ?[ operators now use the existing GenerateNullableTypeConversion that ensures a nullable type isn't converted to a nullable of nullable. Fixes #169 * Promote operands before generating the equality check on ?. and ?[ operators. * No longer consider ?. and ?[ as tokens, because it prevents the proper parsing of an array of a nullable type (e.g. int?[]). * Improve type parsing to support arrays and generics. Fixes #172 * No longer consider ?. and ?[ as tokens, because it prevents the proper parsing of an array of a nullable type (e.g. int?[]). * Improve type parsing to support arrays and generics. Fixes #172
Looks like the following will try to create Nullable for Nullable.
this will throw an exception
The text was updated successfully, but these errors were encountered: