• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    利用noesis.Javascript开源组件.Net中执行javascript脚本

    界面如下:

    然后,click filter button后的UI:

    看,筛选出来了,代码如下(js能调C#写的UserInfo类的各个属性)

    复制代码 代码如下:

    public class UserInfo
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
            public override string ToString()
            {
                return string.Format("{0}, {1}: {2}", FirstName, LastName, Age);
            }
        }

    private void btnSearch_Click(object sender, EventArgs e)
            {
                ListUserInfo> users = FilterUsers(txtFilter.Text.Trim());
                lstUsers.DataSource = users;
            }

            private ListUserInfo> FilterUsers(string jsFilter)
            {
                ListUserInfo> results = new ListUserInfo>();
                if (jsFilter.Trim().Length == 0)
                    return this.users;

                try
                {
                    using (JavascriptContext context = new JavascriptContext())
                    {
                        foreach (UserInfo user in this.users)
                        {
                            context.SetParameter("user", user);
                            context.SetParameter("isInclude", false);
                            string js = string.Format(@"
                                                    if({0})
                                                        isInclude=true;
                                                    else
                                                        isInclude=false;", jsFilter);
                            context.Run(js);
                            bool isInclude = (bool)context.GetParameter("isInclude");
                            if (isInclude)
                                results.Add(user);
                        }
                    }
                }
                catch
                {
                    txtFilter.Focus();
                    txtFilter.SelectAll();
                    return this.users;
                }

                return results;
            }

    我们来看看效率:

    复制代码 代码如下:

    private void btnTest_Click(object sender, EventArgs e)
            {
                using (JavascriptContext context = new JavascriptContext())
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    int i=1000000;
                    while(i-- > 0)
                    {
                        context.SetParameter("ii", 1);
                        context.SetParameter("jj", 2);
                        context.SetParameter("sum", 0);
                        string js = string.Format(@"sum=ii+jj;");
                        context.Run(js);
                        int sum = (int)context.GetParameter("sum");
                    }
                    sw.Stop();

                    long useJS_Total = sw.ElapsedMilliseconds;
                    float useJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                    sw.Reset();
                    sw.Start();
                    i = 1000000;
                    while (i-- > 0)
                    {
                        int ii = 1;
                        int jj = 2;
                        int sum = 0;
                        sum = ii + jj;
                    }
                    sw.Stop();

                    long nouseJS_Total = sw.ElapsedMilliseconds;
                    float nouseJS_Average = ((float)sw.ElapsedMilliseconds) / 1000000;

                    string msg = "";
                    msg += string.Format("useJS_Total: {0}\r\n", useJS_Total);
                    msg += string.Format("useJS_Average: {0}\r\n\r\n\r\n", useJS_Average);
                    msg += string.Format("nouseJS_Total: {0}\r\n", nouseJS_Total);
                    msg += string.Format("nouseJS_Average: {0}\r\n\r\n\r\n", nouseJS_Average);

                    MessageBox.Show(msg);
                }
            }

    结果(上面的是通过js引擎计算的时间、下面的是纯C#计算时间,悲剧啊):这个Noesis.Javascript只适用于性能要求不高的场合

    您可能感兴趣的文章:
    • 详解Chart.js轻量级图表库的使用经验
    • 常用的9个JavaScript图表库详解
    • 使用Chart.js图表库制作漂亮的响应式表单
    • JavaScript可视化图表库D3.js API中文参考
    • javaScript矢量图表库-gRaphael几行代码实现精美的条形图/饼图/点图/曲线图
    • Node.js开源应用框架HapiJS介绍
    • 开源的javascript项目Kissy介绍
    • Java中使用开源库JSoup解析HTML文件实例
    • jQuery EasyUI 开源插件套装 完全替代ExtJS
    • 优秀js开源框架-jQuery使用手册(1)
    • 一个加密JavaScript的开源工具PACKER2.0.2
    • 4个顶级开源JavaScript图表库
    上一篇:有潜在危险的 Request.Form 值避免方法
    下一篇:.net重启iis线程池和iis站点程序代码分享
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    利用noesis.Javascript开源组件.Net中执行javascript脚本 利用,noesis.Javascript,开源,