C#winform Call JS ExecutionJs(Javascript) Methodpre-Class knowledge Reserve:1,Jsthe location where the code is placed. method One: Use Microsoft Official components Interop.msscriptcontrol Scriptcontrolclasssc = new Scriptcontrolclass ();//Declaration VariablesSC. Usesafesubset = true; //allow unsafe code to be executedSC. Language = "JScript"; VBScriptSC. Addcode (Properties.Resources.GetTimes);// properties.resourcesresource File the name of the method to execute,parameters to execute/Write the method name if there is no argumentstring str = sc. Run ("Time", newobject[] {"Time ()"}). ToString ();//Special attentionwhenJSwhen there are no parameters in the method,The method name is passed at this time.Disadvantages: DLL must be carried. x64 is not supported.difference: Method One requires DLL, method two reflection method is not required DLL.method One does not support X64, method two supports.method Two must be wrapped using the "'" quotation mark when the required argument is a string. Method one does not needMethod two: Using reflection to get the component /// <summary> /// GetJStime Stamp -bit// </summary>// <returns></returns>Public stringgettimebyjs () {Type obj = Type.gettypefromprogid ("ScriptControl");if (obj = = null) return null;Object ScriptControl = Activator.CreateInstance (obj);obj. InvokeMember ("Language", BindingFlags.SetProperty, NULL, ScriptControl, newobject[] {"JScript"});string js = "Functiontime () {return new Date (). GetTime ()}";obj. InvokeMember ("Addcode", BindingFlags.InvokeMethod, NULL, ScriptControl, newobject[] {js});return obj. InvokeMember ("Eval", Bindingflags.invokemethod,null, ScriptControl, newobject[] {"Time ()"}). ToString (); }
Have to say the method: inside the environment variable-system variable-path-point edit, added at the end;. NETFramework (note compiled version) absolute path "Note that there is also a semicolon in front of the system variable can not have Chinese, space: O. direct cmd jec the call to compile. For example: "Jsc/t:library xxx.js" |
Video tutorial from: http://www.xuanjics.com/thread-86-1-1.html
Address of the Mystery forum: www.xuanjics.com original King's Landing
QQ Exchange Group: 16885911
JS Call of the foreign chapter