zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
459 stars 86 forks source link

Use DateTime values #23

Closed saitham closed 6 years ago

saitham commented 6 years ago

Hi,

using ExpressionEvaluator.dll Version 2.0.4.0 actually we get DateTimes as a input from our users and would like to perform equality compare and so on.

So how can I declare for the Evaluator that something like '01.01.2017 08:00:00' is a date? Tried allready to use stuff like DateTime.Parse but it did not work (Is there a way to use functions from c# System lib at this version)

regards saitham

JonathanMagnan commented 6 years ago

Hello @saitham ,

Unfortunately, ExpressionEvaluator is no longer supported. They had no many issues and the infrastructure was making nearly impossible to support some more complex syntax such as Extension Methods.

We recommend you to move instead Eval-Expression.NET, it's a paid library but supports nearly everything.

We tested your date with DateTime.Parse and everything seem to work with our library.

// Test with C#
var d1 = DateTime.Parse("01.01.2017 08:00:00");

// Test with Eval-Expression
var d2 = Eval.Execute("DateTime.Parse('01.01.2017 08:00:00')");

// Custom syntax only allowed in our compilator to make easier to work with date.
var d3 = Eval.Execute("#01.01.2017 08:00:00#");

Best Regards,

Jonathan

saitham commented 6 years ago

Thx for your quick response. Moving to Eval-Expression seems reasonable but ExpressionEvaluator is integrated deep inside our software and I can not estimate the changes we will have to make in case of that.

I hoped to find some kind of documentation for ExpressionEvaluator since it moved to get those dates working for now and upgrade/move later...

Best regards

saitham

JonathanMagnan commented 6 years ago

Hello @saitham ,

Yes, I understand you cannot really make this move.

Is there no way for you to cast the date outside of the expression?

I tried a few scenarios, one way to make it work is to create your own converter:

public class MyClass
{
    public DateTime ParseToDate(string s)
    {
        return DateTime.Parse(s);
    }
}

var converter = new MyClass();
var expression = new CompiledExpression("converter.ParseToDate(\"01.01.2017 08:00:00\")");

var registry = new TypeRegistry();
registry.RegisterSymbol("converter", converter);
expression.TypeRegistry = registry;

var result = expression.Eval();

Best Regards,

Jonathan

saitham commented 6 years ago

Hey Jonathan

Thats exactly what I was looking for! Did not saw the wood for the trees. Never thought about a method.

Thank you very much. saitham