只是想知道是否有人尝试过将任何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#更容易(也更安全)。
嘿,在codeplex (http://javascriptdotnet.codeplex.com/)上看看Javascript . net的0.3.1版本,有一些非常棒的新特性,你可能会感兴趣。
查看示例代码:
// Initialize the context
JavascriptContext context = new JavascriptContext();
// Setting the externals parameters of the context
context.SetParameter("console", new SystemConsole());
context.SetParameter("message", "Hello World !");
context.SetParameter("number", 1);
// Running the script
context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");
// Getting a parameter
Console.WriteLine("number: " + context.GetParameter("number"));
我知道我打开了一个旧线程,但我已经在smnet (spidermonkey-dotnet)上做了很多工作。在最近几年。它的主要开发重点是将.net对象无缝嵌入spidermonkey引擎。它支持从js值到.net对象的各种转换。其中包括代表和活动。
只是说,现在可能值得一查,因为它有一些稳定的发展:)。
我保持SVN回购更新bug修复和新功能。源文件和项目解决方案文件已配置为在下载时成功构建。如果在使用过程中有任何问题,请随时与我们讨论。
我确实理解人们对托管javascript解决方案的渴望,但在我使用过的所有托管javascript中,它们都非常缺乏一些关键特性,这些特性有助于使它们既健壮又易于使用。我自己也在等待IronJS成熟一点。当我等待的时候,我玩spidermonkey-dotnet =)
Spidermonkey-dotnet项目和下载页面
编辑:今天下午创建了文档维基页面。
现在有了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应用程序,你可以在离线桌面应用程序中使用它。
试试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
我想出了一个更简单的办法。
我使用Javascript构建了一个.dll文件,然后使用VS2013开发人员命令提示符中提供的Javascript编译器编译它。
一旦我们有了。dll,我们只需将它添加到\Support文件夹,然后在需要评估Javascript语句的项目中引用它。
创建.dll的详细步骤:
Create a file in Notepad with only these contents:
class EvalClass { function Evaluate(expression: String) { return eval(expression); } }
Save the file as C:\MyEval.js
Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)
Type Cd\ to get to C:\
Type
jsc /t:library C:\MyEval.js
A new file is created named MyEval.dll.
Copy MyEval.dll to the project and reference it (also reference Microsoft.Jscript.dll).
Then you should be able to call it like this:
Dim jScriptEvaluator As New EvalClass
Dim objResult As Object
objResult = jScriptEvaluator.Evaluate(“1==1 && 2==2”)
objResult为True。