usingMsscriptcontrol;usingSystem;usingSystem.Collections.Generic;usingSystem.Reflection;usingSystem.Text;usingV8sharp;namespaceJS Call {classProgram {Static voidMain (string[] args) { stringRestring =@"function MessageBox (s) {return ' hellow ' + s; } "; stringMethodName ="MessageBox"; stringPara ="' bbs.msdn5.com '"; //please refer to Interop.MSScriptControl.dll Framework4.0 reference Interop.msscriptcontrol_fr40 First//String Info =runmethod (restring,para,methodname);//Run//String info = Evalmethod (restring, para, MethodName);//Eval stringinfo = V8method (restring, para, MethodName);//V8Console.WriteLine (info); Console.readkey (); } /// <summary> ///Execute the JS V8 method/// </summary> /// <param name= "restring" >JS Code</param> /// <param name= "Para" >parameter strings (separated by commas)</param> /// <param name= "MethodName" >Method Name</param> Static stringV8method (stringRestring,stringParastringMethodName) {V8engine engine= V8engine.create ();//Create a V8 objectV8script script = Engine.compile (restring);//compiling Try{engine. Execute (script);//to load the compiled script into the V8 engine stringres = engine. Execute (string. Format ("{0} ({1})", MethodName, para)). ToString ();//Execution Results returnRes; } Catch(Exception ex) {returnEx. Message;//Exception Information } } /// <summary> ///Execute the JS eval method/// </summary> /// <param name= "restring" >JS Code</param> /// <param name= "Para" >parameter strings (separated by commas)</param> /// <param name= "MethodName" >Method Name</param> Static stringEvalmethod (stringRestring,stringParastringMethodName) { Try{Type obj= Type.gettypefromprogid ("ScriptControl"); if(obj = =NULL)return ""; //To create an object instance using the default constructor ObjectScriptControl =activator.createinstance (obj); //InvokeMember Setting PropertiesObj. InvokeMember ("Language", BindingFlags.SetProperty,NULL, ScriptControl,New Object[] {"JScript" }); Obj. InvokeMember ("Addcode", BindingFlags.InvokeMethod,NULL, ScriptControl,New Object[] {restring}); //executes a method (method name) of the current object InvokeMethod asynchronously executes a method returnObj. InvokeMember ("Eval", BindingFlags.InvokeMethod,NULL, ScriptControl,New Object[] {string. Format ("{0} ({1})", MethodName, para)}). ToString ();//Execution Results } Catch(Exception ex) {stringErrorInfo =string. Format ("Error executing JS: \ r \ n Error description: {0} \ r \ n Error reason: {1} \ r \ n Error Source: {2}", ex. Message, ex. Innerexception.message, ex. Innerexception.source);//Exception Information returnerrorinfo; } } /// <summary> ///Execute the JS Run method/// </summary> /// <param name= "restring" >JS Code</param> /// <param name= "Para" >parameter strings (separated by commas)</param> /// <param name= "MethodName" >Method Name</param> Static stringRunmethod (stringRestring,stringParastringMethodName) {Scriptcontrolclass SC=NewScriptcontrolclass (); Try{SC. Usesafesubset=true;//we will check the meaning of the specific representative by ourselves.Sc. Language ="JScript";//Asp VBSCRITP jscript!= JavaScriptSC. Addcode (restring); string[] str = para. Split (','); Object[] obj =New Object[Str. Length]; for(inti =0; I < Str. Length; i++) {Obj[i]=Str[i]; } returnSc. Run (MethodName, obj). ToString ();//Execution Results } Catch(Exception ex) {if(SC. Error.source! =NULL) { stringTXT = sc. Error.text = =NULL?"": SC. Error.text; stringErrorInfo =string. Format ("Error executing JS: \ r \ n {0} row, {1} column, \ r \ n Error description: {2}: {3};\r\n error code:{4}"Sc. Error.Line.ToString (), SC. Error.Column.ToString (), SC. Error.source, SC. Error.Description.ToString () +txt, SC. Error.Number.ToString ()); //ErrorInfo Exception Information returnTXT +errorinfo; } Else { //Ex. Message; Exception Information returnEx. Message; } } } }}
C#js Call