using System;
using System.Collections.Generic;
using System.Text;
using MSScriptControl;
namespace MyQuery.CSharpScript
{
/// <summary>
/// 腳本運行錯誤代理
/// </summary>
public delegate void RunErrorHandler();
/// <summary>
/// 腳本運行超時代理
/// </summary>
public delegate void RunTimeoutHandler();
/// <summary>
/// 腳本處理引擎
/// by 開源盛世-源代碼下載網 基於網絡文章整理開發,是學習自定義腳本的產物
/// </summary>
public sealed class ScriptEngine
{
private ScriptControlClass msc;
/// <summary>
/// 定義腳本運行錯誤事件
/// </summary>
public event RunErrorHandler RunError;
/// <summary>
/// 定義腳本運行超時事件
/// </summary>
public event RunTimeoutHandler RunTimeout;
/// <summary>
///構造函數
/// </summary>
public ScriptEngine()
: this(ScriptLanguage.JavaScript)
{
}
/// <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(ScriptEngine_Error);
((DScriptControlSource_Event)this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout);
}
/// <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">入口函數名稱</param>
/// <param name="parameters">參數</param>
/// <param name="codeBody">函數體</param>
/// <returns>返回值object</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>
/// 獲取或設置腳本執行時間,單位為毫秒 www.2cto.com
/// </summary>
public int Timeout
{
get { return this.msc.Timeout; }
set { this.msc.Timeout = value; }
}
/// <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();
}
private void ScriptEngine_Error()
{
OnError();
}
private void ScriptEngine_Timeout()
{
OnTimeout();
}
}
/// <summary>
/// 腳本語言枚舉
/// </summary>
public enum ScriptLanguage
{
/// <summary>
/// JScript腳本語言
/// </summary>
JScript,
/// <summary>
/// VBscript腳本語言
/// </summary>
VBScript,
/// <summary>
/// JavaScript腳本語言
/// </summary>
JavaScript
}
}
歡迎訪問:http://121.18.78.216 適易查詢分析、工作流、內容管理及項目管理演示平台
作者 賈世義