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

cast / parentheses causing null coalescing usage failure #157

Closed mrtristan closed 3 months ago

mrtristan commented 5 months ago

stripped down to a relatively minimal block to show the issue.

error: Oops! No applicable member has been found for the expression. The error occurred for expression "?." at position 59 near "?.ToString(\"g\");

const string mutationScript = """
// throws exception
    dict["CreatedOn"] = ((DateTime?)dict["CreatedOn"])?.ToString("g");
// works
    //  dict["CreatedOn"] = dict["CreatedOn"]?.ToString("g");
""";

var dicts = new[] {
    new Dictionary<string, object>()
    {
        { "CreatedOn", DateTime.Now }
    },
    new Dictionary<string, object>()
    {
        { "CreatedOn", null }
    }
};

var evalContext = new EvalContext
{
    SafeMode = true
};
evalContext.UnregisterAll();
evalContext.RegisterDefaultAliasSafe();
evalContext.RegisterType(
    typeof(IDictionary<string, object>),
    typeof(string),
    typeof(DateTime?)
);

var compiled = evalContext.Compile<Action<IDictionary<string, object>>>(
       mutationScript,
       "dict"
      );

foreach(var dict in dicts) {
    compiled(dict); 
}
JonathanMagnan commented 5 months ago

Hello @mrtristan ,

Thank you for reporting; I can confirm I successfully reproduced this issue with your code, so we will look at it.

Best Regards,

Jon

JonathanMagnan commented 4 months ago

Hello @mrtristan ,

The v6.1.4 has been released.

Could you let us know if my developer fixed the issue correctly?

Best Regards,

Jon