Skip to content

parser: throw ArgumentException when Evaluate cannot convert the result - #44

Merged
rameel merged 1 commit into
mainfrom
parser/report-evaluate-conversion-errors
Nov 19, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/report-evaluate-conversion-errors

Conversation

@rameel

@rameel rameel commented Nov 19, 2026

Copy link
Copy Markdown
Owner

After parsing, Evaluate converts the result of the expression to object or TResult. When there was no such conversion, Expression.Convert threw InvalidOperationException, although Evaluate reports an invalid expression with ArgumentException. This happened in all overloads for a call of a void method, such as Evaluate("TimeZoneInfo.ClearCachedData()"), and for a result without a conversion to TResult, such as Evaluate<string>("1").

This PR throws ArgumentException with a message like the C# error CS0030, for example Cannot convert type 'System.Void' to 'System.Object'. The XML comments of the Evaluate overloads now mention this case.

Existing conversions are not affected: Evaluate<int>("1.9") still returns 1, and Evaluate<string>("1 :> object") still throws InvalidCastException when the value cannot be cast.

After parsing, Evaluate converted the result to object or TResult with
Expression.Convert. When there was no such conversion, as for a call
of a void method or for Evaluate<string>("1"), Expression.Convert threw
InvalidOperationException, although Evaluate reports an invalid
expression with ArgumentException.

Throw ArgumentException with a message like the C# error CS0030, such
as "Cannot convert type 'System.Void' to 'System.Object'.", and mention
this case in the XML comments of the Evaluate overloads.
@rameel
rameel merged commit 8598e4a into main Nov 19, 2026
1 check passed
@rameel
rameel deleted the parser/report-evaluate-conversion-errors branch November 19, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant