Implementing JS Eval in C#

╄→尐↘猪︶ㄣ 提交于 2019-11-30 20:44:05

问题


Possible Duplicate:
C# eval equivalent?

Duplicate of How can I evaluate C# code dynamically?

How can we Implement JS eval() in C# If possible provide an example.. thank you


回答1:


You can actually use the JScript eval function from C#...

Create a file JsMath.js, with the following JScript code :

class JsMath
{
    static function Eval(expression : String) : double
    {
        return eval(expression);
    };
}

Compile it into a DLL :

jsc /t:library JsMath.js

Add a reference to JsMath.dll to your project. You can now use the JsMath class in your code :

double result = JsMath.Eval(expression);



回答2:


If you can use C# 3.0 / .NET 3.5, there's a sample of a fully operational expression parser in C# Samples for Visual Studio 2008 at MSDN Code Gallery, under DynamicQuery. This makes it possible not just to evaluate single expressions, but even to create functions (delegates) from them, or LINQ expressions. (The LinqDataSource control uses a slightly modified version of this sample internally.)




回答3:


Go to http://json.org/ and scroll to the bottom. Look for C# in the left column.



来源:https://stackoverflow.com/questions/1260043/implementing-js-eval-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!