C#js Call

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.