zoukankan      html  css  js  c++  java
  • IronJS可变参数的函数

    IronJS在github上wiki上有示例代码说明如何输出一个CLR的函数到JS环境中去,但是他只说明了怎样输出一个固定参数的函数,未提及可变参数的问题。甚至于IronJS.Native.Utils.CreateFunction()必须通过第2个参数将CLR函数的参数个数告知IronJS。

    C#的Console.WriteLine()函数用起来还是比较爽的,如何在JS环境中创建一个print(format, …)函数呢?通过JS函数的arguments可以轻松搞定这件事情,代码如下:

    using System;
    using System.Collections.Generic;
    using IronJS;
    using IronJS.Runtime;
    
    class Script
    {
        static void print(BoxedValue arguments)
        {
            Func<string, object> arg = name =>
            {
                return arguments.Object.Members[name];
            };
    
            int length = (int)(double)arg("length"); // js中只有double
            if (length == 0)
            {
                return;
            }
    
            string format = arg("0").ToString();
            if (length == 1)
            {
                Console.WriteLine(format);
                return;
            }
    
            object[] objs = new object[length - 1];
            for (int i = 1; i < length; i++)
            {
                objs[i - 1] = arg(i.ToString());
            }
            Console.WriteLine(format, objs);       
        }
    
        [STAThread]
        static public void Main(string[] args)
        {
            var ctx = new IronJS.Hosting.CSharp.Context();
    
            ctx.SetGlobal("clrprint",
                IronJS.Native.Utils.CreateFunction<Action<BoxedValue>>(ctx.Environment, 1, print));
            ctx.Execute("function print() { clrprint(arguments); }");
    
            ctx.Execute(@"print('Hello {0} {1}', 'world', 123);");
            Console.ReadKey(false);
        }
    }

    以上代码采用最新版的csscript测试通过,注意JS中只有double,没有int,在转int之前必须先转成double

  • 相关阅读:
    突破极验验证的验证码
    c# 自定义多选下拉列表2
    c#多选下拉框(ComboBox)
    图片缩放+拖动(html)
    使用天天模拟器开发android应用
    FineUI开源版之TreeGrid(修改)
    FineUI开源版之TreeGrid实现
    c# 窗体最小化后截图实现
    c#一个简单的实例告诉你,多继承还可以这么来
    设置控件Enable=false,控件颜色不变
  • 原文地址:https://www.cnblogs.com/windtail/p/2924736.html
Copyright © 2011-2022 走看看