程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 利用noesis.Javascript開源組件.Net中執行javascript腳本

利用noesis.Javascript開源組件.Net中執行javascript腳本

編輯:ASP.NET基礎

界面如下:

然後,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只適用於性能要求不高的場合

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved