ClearScript .NET 脚本工具开源项目

我要开发同款
匿名用户2015年07月07日
18阅读
开发技术C#
所属分类常用工具包、程序开发
授权协议Ms-PL

作品详情

ClearScript可以帮助开发者添加脚本到.NET应用,当前支持JavaScript(V8和JScript)和VBScript。

示例using System;using Microsoft.ClearScript;using Microsoft.ClearScript.V8;// create a script engineusing (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);}

查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论