標籤:
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調用