当前位置: 首页 > news >正文

微软自家的.Net下的JavaScript引擎——ClearScript

之前我介绍过一个开源的.Net下的Javascript引擎Javascript .NET,今天发现微软自己也开源了一个JavaScript引擎——ClearScript(当然,也支持VB Script)。

由于是微软发布的,基本上没有什么好挑剔的地方,稳定而强大,不过不支持WinRT。下面这个则是官方示例,就不做更多介绍了,感兴趣的朋友可以去CodePlex看下。

 1 using System;
 2 using Microsoft.ClearScript;
 3 using Microsoft.ClearScript.V8;
 4 
 5 // create a script engine
 6 using (var engine = new V8ScriptEngine())
 7 {
 8     // expose a host type
 9     engine.AddHostType("Console", typeof(Console));
10     engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");
11 
12     // expose a host object
13     engine.AddHostObject("random", new Random());
14     engine.Execute("Console.WriteLine(random.NextDouble())");
15 
16     // expose entire assemblies
17     engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
18     engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");
19 
20     // create a host object from script
21     engine.Execute(@"
22         birthday = new lib.System.DateTime(2007, 5, 22);
23         Console.WriteLine(birthday.ToLongDateString());
24     ");
25 
26     // use a generic class from script
27     engine.Execute(@"
28         Dictionary = lib.System.Collections.Generic.Dictionary;
29         dict = new Dictionary(lib.System.String, lib.System.Int32);
30         dict.Add('foo', 123);
31     ");
32 
33     // call a host method with an output parameter
34     engine.AddHostObject("host", new HostFunctions());
35     engine.Execute(@"
36         intVar = host.newVar(lib.System.Int32);
37         found = dict.TryGetValue('foo', intVar.out);
38         Console.WriteLine('{0} {1}', found, intVar);
39     ");
40 
41     // create and populate a host array
42     engine.Execute(@"
43         numbers = host.newArr(lib.System.Int32, 20);
44         for (var i = 0; i < numbers.Length; i++) { numbers[i] = i; }
45         Console.WriteLine(lib.System.String.Join(', ', numbers));
46     ");
47 
48     // create a script delegate
49     engine.Execute(@"
50         Filter = lib.System.Func(lib.System.Int32, lib.System.Boolean);
51         oddFilter = new Filter(function(value) {
52             return (value & 1) ? true : false;
53         });
54     ");
55 
56     // use LINQ from script
57     engine.Execute(@"
58         oddNumbers = numbers.Where(oddFilter);
59         Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
60     ");
61 
62     // use a dynamic host object
63     engine.Execute(@"
64         expando = new lib.System.Dynamic.ExpandoObject();
65         expando.foo = 123;
66         expando.bar = 'qux';
67         delete expando.foo;
68     ");
69 
70     // call a script function
71     engine.Execute("function print(x) { Console.WriteLine(x); }");
72     engine.Script.print(DateTime.Now.DayOfWeek);
73 
74     // examine a script object
75     engine.Execute("person = { name: 'Fred', age: 5 }");
76     Console.WriteLine(engine.Script.person.name);
77 }
View Code

  

转载于:https://www.cnblogs.com/TianFang/p/3470230.html

相关文章:

  • 计算码流
  • 深圳街头偶遇写字人
  • html5游戏网站
  • 更新flash builder4,6 的AIR版本
  • 改修jquery支持cmd规范的seajs
  • [SharePoint][SharePoint Designer 入门经典]Chapter13 客户端Silverlight编程
  • vbRichClient5的Collection对象参考
  • 01-导航实例
  • 拉风的服务器监控工具,随时掌握服务器动态,AgileEAS.NET SOA 平台服务器监控工具集介绍...
  • DEL: 华为无线modem变无线路由器
  • windows 2003远程桌面退出后系统自动注销的解决方法
  • 电脑开机后数字键盘为关闭状态
  • HTML DOM Frame 的 src
  • box-shadow的使用
  • [04] Android逐帧动画(一)
  • Angular4 模板式表单用法以及验证
  • ES学习笔记(12)--Symbol
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • learning koa2.x
  • nginx 配置多 域名 + 多 https
  • react-native 安卓真机环境搭建
  • Spring核心 Bean的高级装配
  • VUE es6技巧写法(持续更新中~~~)
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 给第三方使用接口的 URL 签名实现
  • 回顾 Swift 多平台移植进度 #2
  • 前端设计模式
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 设计模式 开闭原则
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • - 转 Ext2.0 form使用实例
  • 阿里云ACE认证之理解CDN技术
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​2021半年盘点,不想你错过的重磅新书
  • #每天一道面试题# 什么是MySQL的回表查询
  • (0)Nginx 功能特性
  • (06)Hive——正则表达式
  • (4)STL算法之比较
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (二)c52学习之旅-简单了解单片机
  • (二)pulsar安装在独立的docker中,python测试
  • (离散数学)逻辑连接词
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)jQuery 基础
  • .gitignore文件—git忽略文件
  • .NET Core 中插件式开发实现
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET命名规范和开发约定
  • .NET中的十进制浮点类型,徐汇区网站设计
  • @hook扩展分析
  • [C/C++] C/C++中数字与字符串之间的转换