1
Vote

Eval CompiledExpression with DateTime errors

description

Trying to do a simple evaluation of:

var expression = new CompiledExpression("DateTime.Today.ToString("dd/MM/yy")");
var result = expression.Eval();
Console.WriteLine(result);

Exits with error: "Unknown type or identifier 'DateTime'"

comments

RupertAvery wrote Oct 15, 2016 at 6:24 PM

You need to set the typeregistry and then call RegisterDefaultTypes()
            var expression = new CompiledExpression("DateTime.Today.ToString(\"dd/MM/yy\")");
            expression.TypeRegistry = new TypeRegistry();
            expression.TypeRegistry.RegisterDefaultTypes();
            var result = expression.Eval();
            Console.WriteLine(result);