关于动态执行代码(js的Eval)
2006-05-14 15:01
495 查看
熟悉javascript的朋友对Eval()函数可能都不会陌生,我们可以用它来实现动态代码的执行,我自己甚至写过一个网页专门用来计算算术表达式的,计算能力上比google、baidu的计算器还要好一些,至少精度要高,但是如果超出了四则运算的话,表达式的形式会复杂很,比如以百度给出的例子:
log((5+5)^2)-3+pi需要写成Math.log(Math.pow(5+5,2))*Math.LOG10E-3+Math.PI才能用Eval进行计算,对于这一点我还没有想到理想的解决方案。好了,这不是本文正题,我们姑且放过。
博客园里曾经见人用过下面的代码,至少从代码形式上挺简单的:
// csc.exe noname1.cs /r:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.JScript.dll
//注:需加入Microsoft.JScript与Microsoft.Vsa两个命名空间。
public class Class1
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World");
string Expression = "var result:int =0;result==1?\"成功\":\"失败\"";
Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
Console.WriteLine(Microsoft.JScript.Eval.JScriptEvaluate(Expression, ve));
}
}
不过,令人不爽的是,编译环境现在给出如下警告:'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%\system32\msscript.ocx ,我将完整的代码直接贴出来。
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>
/// 引用com组件Microsoft Script Control
/// %SystemRoot%\system32\msscript.ocx
/// 该函数用来动态执行代码
/// </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;
}
}
}
log((5+5)^2)-3+pi需要写成Math.log(Math.pow(5+5,2))*Math.LOG10E-3+Math.PI才能用Eval进行计算,对于这一点我还没有想到理想的解决方案。好了,这不是本文正题,我们姑且放过。
博客园里曾经见人用过下面的代码,至少从代码形式上挺简单的:
// csc.exe noname1.cs /r:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.JScript.dll
//注:需加入Microsoft.JScript与Microsoft.Vsa两个命名空间。
public class Class1
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World");
string Expression = "var result:int =0;result==1?\"成功\":\"失败\"";
Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
Console.WriteLine(Microsoft.JScript.Eval.JScriptEvaluate(Expression, ve));
}
}
不过,令人不爽的是,编译环境现在给出如下警告:'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%\system32\msscript.ocx ,我将完整的代码直接贴出来。
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>
/// 引用com组件Microsoft Script Control
/// %SystemRoot%\system32\msscript.ocx
/// 该函数用来动态执行代码
/// </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;
}
}
}
相关文章推荐
- 关于动态执行代码(js的Eval)实例详解
- 关于动态执行代码(js的Eval)
- 关于js动态插入script 标签后页面JS代码执行失败的解决方法
- 如何在net中实现动态执行代码(js的eval)的功能?
- js eval(动态执行命令)
- 关于HTML页面中JS/JQ/Onload执行顺序测试代码
- js中eval函数动态执行语句
- 关于动态加载外部js的执行顺序
- C#动态执行类库中的函数:利用反射实现(js)Eval,asp(Execute)功能
- Javascript动态执行JS(new Function与eval比较)
- 关于jsp、js、html代码执行顺序
- 简易 Javascript DOM 包 | 元素水平垂直居中 | 动态执行 JS 代码 | 获取指定元素
- C#利用反射实现(js)Eval,asp(Execute)功能 动态执行类库中的函数
- eval 是执行一段完整的js字符串代码,并将结果返回
- c# EVAL 动态 执行 代码 .net 执...
- 关于js代码执行顺序
- 使用eval()方法可以实现动态执行js
- 关于在服务器端执行JS代码
- Gridview 中动态嵌入和执行 js 代码
- 关于在jsp中java代码和js以及html代码的执行顺序