How do you do dynamic script evaluation in C #?

What is the state of dynamic code evaluation in C #? For the very advanced feature of the application I'm working on, I would like for users to be able to enter a C # line of code that should be evaluated as logical.

Something like:

DateTime.Now.Hours > 12 && DateTime.Now.Hours < 14 

I want to dynamically evaluate this string and commit the result as a boolean.

I tried Microsoft.JScript.Eval.JScriptEvaluate and it worked, but it is technically outdated and only works with Javascript (not perfect, but workable). In addition, I would like to be able to push objects into the script engine so that they can be used in the evaluation.

Some resources that I find are called dynamically compiled assemblies, but this is more overhead than I think I want to deal with.

So what is the state of the dynamic evaluation script in C #? Is this possible or am I out of luck?

+6
reflection c # dynamic
source share
2 answers

You are using DLR ScriptEngine, here is an example:

http://www.codeproject.com/KB/codegen/ScriptEngine.aspx

+3
source share

The most informative link:

Run string in C # 4.0

Expression trees may also be of interest:

http://community.bartdesmet.net/blogs/bart/archive/2009/08/10/expression-trees-take-two-introducing-system-linq-expressions-v4-0.aspx

Alternatively these links:

+1
source share

All Articles