只是想知道是否有人尝试过将任何js引擎嵌入到。net环境中。我可以找到并实际使用(经过大量的痛苦和努力,因为它相当过时,还没有完全完成)spidermonkey-dotnet项目。有人有这方面的经验吗?引擎像SquirrelFish, V8..

并不是说我对Mozilla的Spidermonkey不满意(在核心ASP中使用它为自定义组件提供类似rails的迷你框架)。NET应用程序),但我仍然想进一步探索这些选项。命令行解决方案不是我所需要的,我不能依赖CLR以外的任何东西,我需要从/到JavaScript/ c#对象调用方法。

// c# class
public class A
{
    public string Hello(string msg)
    {
        return msg + " whatewer";
    }
}

// js snippet
var a = new A();
console.log(a.Hello('Call me')); // i have a console.log implemented, don't worry, it's not a client-side code :)

澄清一下——我并不是试图用服务器端javascript编写应用程序本身。它仅用于编写自定义用户子应用程序(可以看作某种DSL)。让普通人用js编程比用c#更容易(也更安全)。


当前回答

试试ReoScript,一个用c#实现的开源JavaScript解释器。

ReoScript使您的应用程序可以执行JavaScript。它有各种各样的扩展方法,如SetVariable,函数扩展,使用CLR类型,. net事件绑定等。

你好世界:

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.Run(" alert('hello world!'); ");

下面是一个创建winform并显示它的脚本示例。

import System.Windows.Forms.*;        // import namespace

var f = new Form();                   // create form
f.click = function() { f.close(); };  // close when user clicked on form

f.show();                             // show 

其他回答

还有MsieJavaScriptEngine,它使用Internet explorer Chakra引擎

您可能也会对Microsoft ClearScript感兴趣 它托管在GitHub上,并在Ms-Pl许可下发布。

我不是微软的粉丝,但我必须承认V8支持的功能和Javascript差不多。Net,更重要的是,该项目仍在维护。就我而言,对委托的支持也比Spidermonkey-dotnet更好。

ps:它也支持JScript和VBScript,但我们对这个老东西不感兴趣。

ps:它兼容。net 4.0和4.5+

现在有了ASP,这是可能的。Net MVC4 Razor视图引擎。代码是这样的:

// c# class
public class A
{
    public string Hello(string msg)
    {
        return msg + " whatewer";
    }
}

// js snippet
<script type="text/javascript">
var a = new A();
console.log('@a.Hello('Call me')'); // i have a console.log implemented, don't worry, it's not a client-side code :)
</script>

Razor不仅适用于MVC4或其他web应用程序,你可以在离线桌面应用程序中使用它。

开源JavaScript解释器Jint (http://jint.codeplex.com)正是您想要的。

编辑: 该项目已经完全重写,现在托管在Github上https://github.com/sebastienros/jint

试试ReoScript,一个用c#实现的开源JavaScript解释器。

ReoScript使您的应用程序可以执行JavaScript。它有各种各样的扩展方法,如SetVariable,函数扩展,使用CLR类型,. net事件绑定等。

你好世界:

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.Run(" alert('hello world!'); ");

下面是一个创建winform并显示它的脚本示例。

import System.Windows.Forms.*;        // import namespace

var f = new Form();                   // create form
f.click = function() { f.close(); };  // close when user clicked on form

f.show();                             // show