• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

界面如下:

然后,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)
        {
            List<UserInfo> users = FilterUsers(txtFilter.Text.Trim());
            lstUsers.DataSource = users;
        }

        private List<UserInfo> FilterUsers(string jsFilter)
        {
            List<UserInfo> results = new List<UserInfo>();
            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只适用于性能要求不高的场合


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
有潜在危险的 Request.Form 值避免方法发布时间:2022-02-05
下一篇:
.net重启iis线程池和iis站点程序代码分享发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap