C#中v8引擎
2022/3/9 11:44:39
本文主要是介绍C#中v8引擎,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
V8引擎的样例。来源于ClearScript的github上的例子。
以下例子足以满足大部分需求。
using System; using Microsoft.ClearScript; using Microsoft.ClearScript.JavaScript; 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); // read a JavaScript typed array engine.Execute("values = new Int32Array([1, 2, 3, 4, 5])"); var values = (ITypedArray<int>)engine.Script.values; Console.WriteLine(string.Join(", ", values.ToArray())); }
这篇关于C#中v8引擎的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具