月度存档: 十月 2008

[转]An Eval Function for C# using JScript.NET (JavaScript)

Once I was faced with a scenario where we needed client specific calculations applied to the results of a SQL query. The situation cried out for a “eval” type method where we could just plug values into a custom equation and retrieve a result. VBScript, JScript.NET, and JavaScript all contain an eval method, where you can pass a string containing a valid expression for the language and retrieve a result – dynamic execution.

Unfortunately, C# does not provide an eval method. One solution is to add a JScript.NET project to your solution. It feels a bit messy to suddenly have an extra assembly to build and deploy just for 10 lines of JScript code. An alternate solution is to use the System.CodeDom namespace to compile a JScript class in memory to use. 继续阅读 »