91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2021-07-24 14:58:58 來源:億速云 閱讀:443 作者:chen 欄目:開發技術

本篇內容介紹了“如何利用noesis.Javascript開源組件.Net中執行javascript腳本”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

界面如下:

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

然后,click filter button后的UI:

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

看,篩選出來了,代碼如下(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只適用于性能要求不高的場合

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

“如何利用noesis.Javascript開源組件.Net中執行javascript腳本”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

修文县| 右玉县| 威海市| 侯马市| 准格尔旗| 图木舒克市| 叶城县| 翁源县| 东辽县| 广南县| 鹿泉市| 平阴县| 玉溪市| 台中县| 林西县| 浑源县| 泊头市| 开封县| 资溪县| 海安县| 安达市| 辽阳市| 锡林郭勒盟| 浠水县| 雷州市| 重庆市| 泾阳县| 德庆县| 普兰店市| 靖远县| 岳池县| 尚志市| 玛曲县| 鲁甸县| 高淳县| 都安| 梨树县| 清新县| 武乡县| 太和县| 平塘县|