欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# : 后端调用js

程序员文章站 2022-06-19 15:50:39
C# 调用 js Nuget 引用 Microsoft.ClearScript...

C# 调用 js

Nuget

Microsoft.ClearScript

Github

https://github.com/Microsoft/ClearScript

例子

using System;

using Microsoft.ClearScript;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.V8;

namespace TestNet5
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using var engine = new V8ScriptEngine();
            // 暴露 C# 类型         expose a host type
            engine.AddHostType("Console", typeof(Console));
            engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

            // 暴露 C# 对象         expose a host object
            engine.AddHostObject("random", new Random());
            engine.Execute("Console.WriteLine(random.NextDouble())");

            // 暴露 整个C#程序集       expose entire assemblies
            engine.AddHostObject("lib", new HostTypeCollection("mscorlib", "System.Core"));
            engine.Execute("Console.WriteLine(lib.System.DateTime.Now)");

            // 从脚本创建一个C#对象      create a host object from script
            engine.Execute(@"
                birthday = new lib.System.DateTime(2007, 5, 22);
                Console.WriteLine(birthday.ToLongDateString());
            ");

            // 使用脚本中的C#泛型类      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);
            ");

            // 调用带有输出参数的C#方法        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);
            ");

            // 创建并填充一个C#数组      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;
                });
            ");

            // js使用LINQ            use LINQ from script
            engine.Execute(@"
                oddNumbers = numbers.Where(oddFilter);
                Console.WriteLine(lib.System.String.Join(', ', oddNumbers));
            ");

            // 使用动态C#对象         use a dynamic host object
            engine.Execute(@"
                expando = new lib.System.Dynamic.ExpandoObject();
                expando.foo = 123;
                expando.bar = 'qux';
                delete expando.foo;
            ");

            // 调用js函数           call a script function
            engine.Execute("function test(x) { Console.WriteLine(x); return x+'123456' }");
            var res = engine.Script.test("哈哈哈哈");
            Console.WriteLine(res); // 打印js 返回值

            // 获取js对象             examine a script object
            engine.Execute("person = { name: 'Fred', age: 5 }");
            Console.WriteLine(engine.Script.person.name);


            // 读取一个js类型的数组 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()));

            Console.ReadLine();
        }
    }
}

本文地址:https://blog.csdn.net/qq_37214567/article/details/111146319