Jurassic.ScriptEngine 使用
Jurassic.ScriptEngine是一个让net动态执行js的一个引擎。类似的有ironjs等。支持ECMAScript 5,非线程安全
使用
- using Jurassic;
//1简单的执行js字符串
js:
function main(a,b)
{
return a+b;
}
c#
var engine = new Jurassic.ScriptEngine(); engine.Evaluate("上面的js代码");// var addResult= engine.CallGlobalFunction(" main", 5, 6);//结果11
//2加载 js库文件然后执行 js函数
1 |
var engine = new Jurassic.ScriptEngine(); |
// 先加载js文件 然后执行js库的函数
//3 设置全局变量
engine.SetGlobalValue("interop", 15);
//4获取全局变量
engine.GetGlobalValue<int>("interop")
//5 从js中调用net的方法
engine.SetGlobalFunction("test", new Func<int, int, int>((a, b) => a + b));//设置js全局函数test
engine.Evaluate<int>("test(5, 6)") //调用js函数test
调用的时候注意js和net的类型对应关系
C# type name .NET type name JavaScript type name
bool System.Boolean boolean
int System.Int32 number
double System.Double number
string System.String string
Jurassic.Null Jurassic.Null null
Jurassic.Undefined Jurassic.Undefined undefined
Jurassic.Library.ObjectInstance (or a derived type) Jurassic.Library.ObjectInstance (or a derived type) object
//6 net调用js的方法
engine.Evaluate("function test(a, b) { return a + b }");
engine.CallGlobalFunction<int>("test", 5, 6);
//7 暴露net的class给js
using Jurassic;
using Jurassic.Library; public class AppInfo : ObjectInstance
{
public AppInfo(ScriptEngine engine)
: base(engine)
{
// 重写name属性
this["name"] = "Test Application";
// 只读属性
this.DefineProperty("version", new PropertyDescriptor(5, PropertyAttributes.Sealed), true);
}
}
engine.SetGlobalValue("appInfo", new AppInfo(engine));
Console.WriteLine(engine.Evaluate<string>("appInfo.name + ' ' + appInfo.version"));
// 8 暴露net的class的静态方法
using Jurassic;
using Jurassic.Library; public class Math2 : ObjectInstance
{
public Math2(ScriptEngine engine)
: base(engine)
{
this.PopulateFunctions();
} [JSFunction(Name = "log10")]
public static double Log10(double num)
{
return Math.Log10(num);
}
}
engine.SetGlobalValue("math2", new Math2(engine));
engine.Evaluate<double>("math2.log10(1000)");
// 9 暴露net的类实例
using Jurassic;
using Jurassic.Library; public class RandomConstructor : ClrFunction
{
public RandomConstructor(ScriptEngine engine)
: base(engine.Function.InstancePrototype, "Random", new RandomInstance(engine.Object.InstancePrototype))
{
} [JSConstructorFunction]
public RandomInstance Construct(int seed)
{
return new RandomInstance(this.InstancePrototype, seed);
}
} public class RandomInstance : ObjectInstance
{
private Random random; public RandomInstance(ObjectInstance prototype)
: base(prototype)
{
this.PopulateFunctions();
this.random = new Random(0);
} public RandomInstance(ObjectInstance prototype, int seed)
: base(prototype)
{
this.random = new Random(seed);
} [JSFunction(Name = "nextDouble")]
public double NextDouble()
{
return this.random.NextDouble();
}
}
engine.SetGlobalValue("Random", new RandomConstructor(engine));
engine.Evaluate<double>("var rand = new Random(1000); rand.nextDouble()");
备注: 如果同cs-script配合使用,就可以同时动态执行js和net的cs代码,互相调用。
Jurassic.ScriptEngine 使用的更多相关文章
- 利用Jurassic在.net下运行js函数
static void Main(string[] args) { var eng = new Jurassic.ScriptEngine(); eng.Evaluate("function ...
- C#执行javascript代码,执行复杂的javascript代码新方式
1. 使用nuget 包"Jurassic", 注意,如果 nuget上的包 用起来出现错误,请自行下载 github代码,自行编译最新代码成dll,再引用. 官方的nuget包 ...
- JS学习十四天----server端运行JS代码
server端运行JS代码 话说,当今不在client使用JS代码才是稀罕事.因为web应用的体验越来越丰富,client用JS实现的逻辑也越来越多,这造成的结果就是某些差点儿一致的逻辑须要在clie ...
- 测试了几款 C# 脚本引擎 , Jint , Jurassic , Nlua, ClearScript
测试类 public class Script_Common { public string read(string filename) { return System.IO.File.ReadAll ...
- UVALive - 2965 Jurassic Remains (LA)
Jurassic Remains Time Limit: 18000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Sub ...
- java ScriptEngine 使用 (支持JavaScript脚本,eval()函数等)
Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...
- 用ScriptEngine在java中和javascript交互的例子(JDK6新特性)
package demo7; import java.util.Arrays; import java.util.List; import javax.script.Invocable; import ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- 【中途相遇+二进制】【NEERC 2003】Jurassic Remains
例题25 侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...
随机推荐
- HDU 2199 Can you solve this equation?(二分精度)
HDU 2199 Can you solve this equation? Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == ...
- python调用R语言,关联规则可视化
首先当然要配置r语言环境变量什么的 D:\R-3.5.1\bin\x64; D:\R-3.5.1\bin\x64\R.dll;D:\R-3.5.1;D:\ProgramData\Anaconda3\L ...
- Windows RDP远程连接CentOS 7
1. 打开已经安装了CentOS7的主机,以root用户登录,在桌面上打开一个终端,输入命令:rpm -qa|grep epel,查询是否已经安装epel库(epel是社区强烈打造的免费开源发行软 ...
- C++11奇怪的语法
1. istream_iterator 简而言之,istream_iterator像操作容器一样操作istream.例如下面代码,从std::cin构造std::istream_iteream< ...
- MS SQL 流程控制语句
Declare myCursor cursor For Select * from table1 open myCursor Fet ...
- Mysql 查询列名
#列名 select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yunpiaobox_db' and table_ ...
- 在MetaFile里放图片
procedure TForm1.Button1Click(Sender: TObject); var m : TmetaFile; mc : TmetaFileCanvas; b : tbitmap ...
- Python运维开发基础08-文件基础
一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...
- Redis 授权操作
[Redis 授权操作] AUTH password 通过设置配置文件中 requirepass 项的值(使用命令 CONFIG SET requirepass password ),可以使用密码来保 ...
- ICG游戏:斐波那契博弈
描述: 有一堆个数为n(n>=2)的石子,游戏双方轮流取石子,规则如下: 1)先手不能在第一次把所有的石子取完,至少取1颗: 2)之后每次可以取的石子数至少为1,至多为对手刚取的石子数的2倍: ...