Friends who are familiar with JavaScript may not be unfamiliar with the Eval() function. We can use it to implement the execution of dynamic code. I even wrote a web page that is specially used to calculate arithmetic expressions. The calculation ability is better than that of Google and Baidu calculators, at least with higher accuracy. However, if the four operations exceed the four operations, the form of the expression will be very complicated, such as the example given by Baidu:
log((5+5)^2)-3+pi needs to be written as Math.log(Math.pow(5+5,2))*Math.LOG10E-3+Math.PI to be used for calculations with Eval. I have not thought of an ideal solution for this. OK, this is not the main topic of this article, let’s just let it go.
I have seen people use the following code in the blog park, at least in terms of code form, it is quite simple:
// csc.exe noname1.cs /r:C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Microsoft.JScript.dll //Note: Two namespaces of Microsoft.JScript and Microsoft.Vsa need to be added. public class Class1{ static void Main(string[] args) { System.Console.WriteLine("Hello World"); string Expression = "var result:int =0;result==1?/"Success/":/"Failed/""; Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine(); Console.WriteLine(Microsoft.JScript.Eval.JScriptEvaluate(Expression, ve)); }}However, it is unpleasant that the compilation environment now gives the following warning: 'Microsoft.JScript.Vsa.VsaEngine' is obsolete: 'Use of this type is not recommended because it is being deprecated in Visual Studio 2005; there will be no replacement for this feature. Please see the ICodeCompiler documentation for additional help.' Of course, the code can be compiled and executed normally.
Below I will give another method to directly use javascript's Eval function. With the help of the com component, the reference path is %SystemRoot%/system32/msscript.ocx, and I will post the complete code directly.
using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;namespace ScriptProgramming{ class Program { static void Main(string[] args) { string strExpression = "1+2*3"; string strResult = Eval(strExpression); Console.WriteLine(strExpression + "=" + strResult); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } /// <summary> /// Reference com component Microsoft Script Control /// %SystemRoot%/system32/msscript.ocx /// This function is used to execute code dynamically /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static string Eval(string Expression) { string strResult = null; try { MSScriptControl.ScriptControlClass jscript = new MSScriptControl.ScriptControlClass(); jscript.Language = "JScript"; strResult = jscript.Eval(Expression).ToString(); } catch (Exception ex) { Debug.Fail(ex.Message); } return strResult; } }}The above detailed explanation of dynamic code execution (Eval of js) examples is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.