c#JS調用

來源:互聯網
上載者:User

標籤:

using MSScriptControl;using System;using System.Collections.Generic;using System.Reflection;using System.Text;using v8sharp;namespace Js調用{    class Program    {        static void Main(string[] args)        {                string reString = @"            function MessageBox(s)             {                 return ‘Hellow ‘ + s;            }            ";            string MethodName = "MessageBox";            string para = "‘bbs.msdn5.com‘";            //請先引用 Interop.MSScriptControl.dll Framework4.0 引用 Interop.MSScriptControl_fr40            // string info =RunMethod(reString,para,MethodName); //Run            //string info = EvalMethod(reString, para, MethodName); //Eval            string info = V8Method(reString, para, MethodName); //V8            Console.WriteLine(info);            Console.ReadKey();        }        /// <summary>        /// 執行js V8方法        /// </summary>        /// <param name="reString">Js代碼</param>        /// <param name="para">參數字串(使用逗號分隔)</param>        /// <param name="MethodName">方法名稱</param>        static string V8Method(string reString, string para, string MethodName)        {            V8Engine engine = V8Engine.Create();//建立V8對象            V8Script script = engine.Compile(reString);//編譯            try            {                engine.Execute(script);//將編譯的指令碼載入到V8引擎中                string res = engine.Execute(string.Format("{0}({1})", MethodName, para)).ToString();//執行結果                return res;            }            catch (Exception ex)            {              return  ex.Message;//異常資訊            }        }        /// <summary>        /// 執行js Eval方法        /// </summary>        /// <param name="reString">Js代碼</param>        /// <param name="para">參數字串(使用逗號分隔)</param>        /// <param name="MethodName">方法名稱</param>       static  string EvalMethod(string reString, string para, string MethodName)        {            try            {                Type obj = Type.GetTypeFromProgID("ScriptControl");                if (obj == null) return "";                //使用預設建構函式建立對象執行個體                object ScriptControl = Activator.CreateInstance(obj);                // InvokeMember 設定屬性                obj.InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JScript" });                obj.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { reString });                //執行當前對象的方法(方法名) InvokeMethod 非同步執行某一個方法               return obj.InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { string.Format("{0}({1})", MethodName, para) }).ToString();//執行結果            }            catch (Exception ex)            {                string ErrorInfo = string.Format("執行JS出現錯誤:   \r\n 錯誤描述: {0} \r\n 錯誤原因: {1} \r\n 錯誤來源:{2}", ex.Message, ex.InnerException.Message, ex.InnerException.Source);//異常資訊                return ErrorInfo;            }        }         /// <summary>        /// 執行js Run方法        /// </summary>        /// <param name="reString">Js代碼</param>        /// <param name="para">參數字串(使用逗號分隔)</param>        /// <param name="MethodName">方法名稱</param>        static string RunMethod(string reString, string para, string MethodName)        {            ScriptControlClass sc = new ScriptControlClass();            try            {                sc.UseSafeSubset = true;//大家自行查詢下具體代表的含義.                sc.Language = "JScript"; //Asp vbScritp   JScript!= JavaScript                sc.AddCode(reString);                 string[] str = para.Split(‘,‘);                object[] obj = new object[str.Length];                for (int i = 0; i < str.Length; i++)                {                    obj[i] = str[i];                }                            return sc.Run(MethodName, obj).ToString(); //執行結果            }            catch (Exception ex)            {                if (sc.Error.Source != null)                {                    string txt = sc.Error.Text == null ? "" : sc.Error.Text;                    string ErrorInfo = string.Format("執行JS出現錯誤:   \r\n 第{0}行,第{1}列, \r\n 錯誤描述: {2}:    {3};\r\n 錯誤Code:{4}", sc.Error.Line.ToString(), sc.Error.Column.ToString(), sc.Error.Source, sc.Error.Description.ToString() + txt, sc.Error.Number.ToString());                   //  ErrorInfo  異常資訊                    return txt + ErrorInfo;                }                else                {                   // ex.Message; 異常資訊                    return ex.Message;                }            }        }    }}

 

c#JS調用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.