Javascript has an eval method. Anyone who has used it knows its convenience and strength.
In C #, we can also use the COM component to execute JavaScript code.
The following code demonstrates how to use the msscriptcontrol component to execute a mathematical expression:
Msscriptcontrol. scriptcontrolclass SC = new msscriptcontrol. scriptcontrolclass ();
SC. Language = "JavaScript ";
Object OBJ = SC. eval ("1 + 2*(3 + 4 )");
Console. writeline (OBJ );
To use msscriptcontrol, you must reference the COM component Microsoft Script Control 1.0.
Another method is as follows:
// Csc.exe noname1.cs/R: C: \ WINDOWS \ Microsoft. NET \ framework \ v1.1.4322 \ Microsoft. jscript. dll
// Note: the Microsoft. JScript and Microsoft. VSA namespaces must be added.
Public class class1
{
Static void main (string [] ARGs)
{
System. Console. writeline ("Hello World ");
String expression = "Var result: Int = 0; Result = 1? \ "Success \": \ "Failure \"";
Microsoft. JScript. VSA. vsaengine VE = Microsoft. JScript. VSA. vsaengine. createengine ();
Console. writeline (Microsoft. JScript. Eval. jscriptevaluate (expression, VE ));
}
}