Eval-Expression.NET icon indicating copy to clipboard operation
Eval-Expression.NET copied to clipboard

cast / parentheses causing null coalescing usage failure

Open mrtristan opened this issue 1 year ago • 1 comments

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);	
}

mrtristan avatar Apr 30 '24 21:04 mrtristan

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 avatar Apr 30 '24 22:04 JonathanMagnan

Hello @mrtristan ,

The v6.1.4 has been released.

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

Best Regards,

Jon

JonathanMagnan avatar May 21 '24 14:05 JonathanMagnan