I. Using Msscriptcontrol to execute JavaScript code in C # JavaScript There is an eval method used by people who know his convenience and strength.
In C #, we can also execute a piece of JavaScript code through a COM component.
The following code shows how to execute a mathematical expression with the Msscriptcontrol component:
Msscriptcontrol.scriptcontrolclass sc = new Msscriptcontrol.scriptcontrolclass (); SC. Language = "JavaScript"; object obj = sc. Eval ("500-(110-100)/2*6"); Console.WriteLine (obj);
To use Msscriptcontrol, you need to reference the COM component of Microsoft Script Control 1.0.
Two, reference Microsoft.JScript and Microsoft.Vsa two namespaces, and call the Jscriptevaluate method implementation
Note: You need to add microsoft.jscript and Microsoft.Vsa two namespaces. (OBSOLETE)
<summary>//Four mixed operation///</summary>///<param name= "sexpression" > Calculation expression </param>///< Returns> calculation results </returns>///<example>///<code>///object x = Mathx.eval ("500-(110-100)/2*6"); </code>///</example> public static Object Eval (String sexpression) { Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine (); Return Microsoft.JScript.Eval.JScriptEvaluate (Sexpression, ve);}
C # Calculation Expressions (Imitation calculator function)