.net 开源 JavaScript 解析引擎
1. Javascript .NET
地址为:http://javascriptdotnet.codeplex.com/
使用方法:
Quick Start
This section provides documentation to get quickly started to embed and run Javascript .NET in your application.
Download Javascript .NET
Download the Javascript .NET latest binary release here. The contained Noesis.Javascript.dll assembly and the Microsoft Runtime Library files in the same directory are all that is required to run Javascript .NET.
Create a new project
Open Visual Studio and create a new Console application.
Reference the Noesis.Javascript assembly from your project
Add a reference to Noesis.Javascript.dll in your project by right clicking on the project and choosing "Add Reference", then select the "Browse" tab, locate the appropriate Noesis.Javascript.dll assembly and click "OK".
Using Noesis.Javascript
Add the following to the "using" statements at the top of you program file:
using Noesis.Javascript;
Initialize a Javascript context
JavascriptContext context = new JavascriptContext()
(Don't forget to Dispose() this object when you are finished with it.)
Set variables in the Javascript context's global scope: SetParameter(string iName, Object iObject);
context.SetParameter("console", new SystemConsole()); context.SetParameter("message", "Hello World !\n");
Get a parameter from the Javascript context: GetParameter(string iName);
context.GetParameter("number");
Compile and run the Javascript: Run(string iScript);
context.Run("var i; for (i = 0; i < 5; i++) console.Print(message + ' (' + i + ')'); number += i;");
"Hello World" quick integration sample
A simple integration is shown below. In the sample, a context is created to which a CLI object is supplied to expose the console to the Javascript. A string and an integer are also supplied on which elementary operations are performed. Finally, the resulting value of the supplied integer value is extracted and printed to the console.
Code
class Program { public class SystemConsole { public SystemConsole() { } public void Print(string iString) { Console.WriteLine(iString); } } static void Main(string[] args) { // Initialize the context using (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")); } } }
Output
The following will be outputed by the program above:
Hello World ! (0) Hello World ! (1) Hello World ! (2) Hello World ! (3) Hello World ! (4) 6
2. ClearScript
微软自家的地址为:http://clearscript.codeplex.com/
使用方法:
using System; using Microsoft.ClearScript; using Microsoft.ClearScript.V8; // create a script engine using (var engine = new V8ScriptEngine()) { // expose a host type engine.AddHostType("Console", typeof(Console)); engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)"); // expose a host object engine.AddHostObject("random", new Random()); engine.Execute("Console.WriteLine(random.NextDouble())"); // expose entire assemblies engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core")); engine.Execute("Console.WriteLine(lib.System.DateTime.Now)"); // create a host object from script engine.Execute(@" birthday = new lib.System.DateTime(2007, 5, 22); Console.WriteLine(birthday.ToLongDateString()); "); // use a generic class from script engine.Execute(@" Dictionary = lib.System.Collections.Generic.Dictionary; dict = new Dictionary(lib.System.String, lib.System.Int32); dict.Add('foo', 123); "); // call a host method with an output parameter engine.AddHostObject("host", new HostFunctions()); engine.Execute(@" intVar = host.newVar(lib.System.Int32); found = dict.TryGetValue('foo', intVar.out); Console.WriteLine('{0} {1}', found, intVar); "); // create and populate a host array engine.Execute(@" numbers = host.newArr(lib.System.Int32, 20); for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; } Console.WriteLine(lib.System.String.Join(', ', numbers)); "); // create a script delegate engine.Execute(@" Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean); oddFilter = new Filter(function(value) { return (value & 1) ? true : false; }); "); // use LINQ from script engine.Execute(@" oddNumbers = numbers.Where(oddFilter); Console.WriteLine(lib.System.String.Join(', ', oddNumbers)); "); // use a dynamic host object engine.Execute(@" expando = new lib.System.Dynamic.ExpandoObject(); expando.foo = 123; expando.bar = 'qux'; delete expando.foo; "); // call a script function engine.Execute("function print(x) { Console.WriteLine(x); }"); engine.Script.print(DateTime.Now.DayOfWeek); // examine a script object engine.Execute("person = { name: 'Fred', age: 5 }"); Console.WriteLine(engine.Script.person.name); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
2014-06-03 sql server CLR