本文不是基于B/S的 后台调用前台js方法,而是给你一段js方法字符串,让你在程序中直接解析这段方法,并调用方法得到想要的值。
首先要解析Js方法,可以用微软的msscript.ocx控件(Interop.MSScriptControl.dll)来解析js方法.
1.msscript.ocx下载的地址 http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033
或者点击此处下载 Interop.MSScriptControl.dll
2.项目中引用 msscript.ocx 或者 Interop.MSScriptControl.dll
现在通过控件的一些方法可以解析js方法,并调用js方法返回值。
不过这儿可以将控件类的一些属性和方法封装成单独的类,方便自己调用。 封装成ScriptEngine类。
using System;
using MSScriptControl;
using System.Text;
//导入js文件,导入js 方法字符串,然后执行js方法。 通过msscript.ocx控件执行JS脚本 ,先引用 Interop.MSScriptControl.dll
//官网下载: 然后 引用 xxx.ocx就行了 http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033
namespace csharp中执行js方法
{
class script_run
{
/// <summary>
/// 脚本类型
/// </summary>
public enum ScriptLanguage
{
/// <summary>
/// JScript脚本语言
/// </summary>
JScript,
/// <summary>
/// VBscript脚本语言
/// </summary>
VBscript,
/// <summary>
/// JavaScript脚本语言
/// </summary>
JavaScript
}
/// <summary>
/// 脚本运行错误代理
/// </summary>
public delegate void RunErrorHandler();
/// <summary>
/// 脚本运行超时代理
/// </summary>
public delegate void RunTimeoutHandler();
/// <summary>
/// ScriptEngine类
/// </summary>
public class ScriptEngine
{
private ScriptControl msc;
/// <summary>
/// 定义脚本运行错误事件
/// </summary>
public event RunErrorHandler RunError;
/// <summary>
/// 定义脚本运行超时事件
/// </summary>
public event RunTimeoutHandler RunTimeout;
/// <summary>
///构造函数 默认为 VBscript 脚本类型
/// </summary>
public ScriptEngine()
: this(ScriptLanguage.VBscript)
{ }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="language">脚本类型</param>
public ScriptEngine(ScriptLanguage language)
{
this.msc = new ScriptControlClass();
this.msc.UseSafeSubset = true;
this.msc.Language = language.ToString();
((DScriptControlSource_Event)this.msc).Error += new DScriptControlSource_ErrorEventHandler(OnError);
((DScriptControlSource_Event)this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(OnTimeout);
}
/// <summary>
/// 运行Eval方法
/// </summary>
/// <param name="expression">表达式</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Eval(string expression, string codeBody)
{
msc.AddCode(codeBody);
return msc.Eval(expression);
}
/// <summary>
/// 运行Eval方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="expression">表达式</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Eval(ScriptLanguage language, string expression, string codeBody)
{
if (this.Language != language)
this.Language = language;
return Eval(expression, codeBody);
}
/// <summary>
/// 运行Run方法
/// </summary>
/// <param name="mainFunctionName">入口函数名称 例如:add</param>
/// <param name="parameters">参数:例如:new object(){"",""}</param>
/// <param name="codeBody">函数体 例如:fucniton add(int a,int b){return a+b;}</param>
/// <returns>返回值object:获取object.Tostring()</returns>
public object Run(string mainFunctionName, object[] parameters, string codeBody)
{
this.msc.AddCode(codeBody);
return msc.Run(mainFunctionName, parameters);
}
/// <summary>
/// 运行Run方法
/// </summary>
/// <param name="language">脚本语言</param>
/// <param name="mainFunctionName">入口函数名称</param>
/// <param name="parameters">参数</param>
/// <param name="codeBody">函数体</param>
/// <returns>返回值object</returns>
public object Run(ScriptLanguage language, string mainFunctionName, object[] parameters, string codeBody)
{
if (this.Language != language)
this.Language = language;
return Run(mainFunctionName, parameters, codeBody);
}
/// <summary>
/// 放弃所有已经添加到 ScriptControl 中的 Script 代码和对象
/// </summary>
public void Reset()
{
this.msc.Reset();
}
/// <summary>
/// 获取或设置脚本语言
/// </summary>
public ScriptLanguage Language
{
get { return (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage), this.msc.Language, false); }
set { this.msc.Language = value.ToString(); }
}
/// <summary>
/// 获取或设置脚本执行时间,单位为毫秒
/// </summary>
public int Timeout
{
get { return 0; }
}
/// <summary>
/// 设置是否显示用户界面元素
/// </summary>
public bool AllowUI
{
get { return this.msc.AllowUI; }
set { this.msc.AllowUI = value; }
}
/// <summary>
/// 宿主应用程序是否有保密性要求
/// </summary>
public bool UseSafeSubset
{
get { return this.msc.UseSafeSubset; }
set { this.msc.UseSafeSubset = true; }
}
/// <summary>
/// RunError事件激发
/// </summary>
private void OnError()
{
if (RunError != null)
RunError();
}
/// <summary>
/// OnTimeout事件激发
/// </summary>
private void OnTimeout()
{
if (RunTimeout != null)
RunTimeout();
}
}
}
}
开始解析js方法。
假如有个js方法: function add(var a){return a+1;}
通过上面的ScriptEngine类调用 js:add 方法
*********************************************************************
//声明是Javascript方法
script_run.ScriptEngine myScript = new script_run.ScriptEngine(script_run.ScriptLanguage.JavaScript);
//js字符串
string javascriptString=”function add(var a){return a+1;} function test(){}”;
object jsmethodRunValue = myScript.Run("add", new object[] {“3”}, javascriptString);
Console.WriteLine(jsmethodRunValue.ToString());
Console.ReadLine();
*********************************************************************************