How can I evaluate a C# expression dynamically?

后端 未结 7 1329
情话喂你
情话喂你 2020-11-22 07:22

I would like to do the equivalent of:

object result = Eval(\"1 + 3\");
string now    = Eval(\"System.DateTime.Now().ToString()\") as string

7条回答
  •  無奈伤痛
    2020-11-22 08:24

    Looks like there is also a way of doing it using RegEx and XPathNavigator to evaluate the expression. I did not have the chance to test it yet but I kind of liked it because it did not require to compile code at runtime or use libraries that could not be available.

    http://www.webtips.co.in/c/evaluate-function-in-c-net-as-eval-function-in-javascript.aspx

    I'll try it and tell later if it worked. I also intend to try it in Silverlight, but it is too late and I'm almost asleep to do it now.

提交回复
热议问题