不过,令人不爽的是,编译环境现在给出如下警告:'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.'当然,代码可以编译通过,且执行是正常的。
下面我给出另外一种直接使用javascript的Eval函数的方法,借助于com组件,引用路径是 %SystemRoot%system32msscript.ocx ,我将完整的代码直接贴出来。
using System;
using System.Collections.Generic;
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/)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
/// 引用com组件Microsoft Script Control
/// %SystemRoot%system32msscript.ocx
/// 该函数用来动态执行代码
/// /summary
/// param name="Expression"/param
/// returns/returns
public static string Eval(string Expression)
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/webkaifa/){
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;
}
}
}