} /// <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 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(); } } } 上面的包裝定義了一個ScriptLanguage枚舉,這樣操作起來更方便一點。另外腳本引擎包括了Error事件和Timeout事件,根據實際使用情況可進行注冊。 二.腳本引擎演示 我建了個窗體程序,測試包括腳本語言的選擇,是否開啟AllowUI屬性,超時時間的設置,以及腳本引擎調用方法的選擇。測試程序代碼比較長,下面列出了主要部分:using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ZZ { public class Form1 : System.Windows.Forms.Form { private ScriptEngine scriptEngine; private System.Windows.Forms.CheckBox checkBoxAllowUI; private System.Windows.Forms.TextBox textBoxResult; private System.Windows.Forms.NumericUpDown numericUpDownTimeout; private System.Windows.Forms.TextBox textBoxCodeBody; private System.Windows.Forms.Button buttonRun; private System.Windows.Forms.Button buttonCancel; private System.Windows.Forms.ComboBox comboBoxScript; private System.Windows.Forms.TextBox textBoxParams; private System.Windows.Forms.RadioButton radioButtonEval; private System.Windows.Forms.RadioButton radioButtonRun; private System.Windows.Forms.TextBox textBoxMethodName; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); this.comboBoxScript.SelectedIndex = 0; this.scriptEngine = new ScriptEngine(); this.scriptEngine.UseSafeSubset = true; this.scriptEngine.RunError += new RunErrorHandler(scriptEngine_RunError);this.scriptEngine.RunTimeout += new RunTimeoutHandler(scriptEngine_RunTimeout); } protected override void Dispose( bool disposing ) { if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); } #region Windows 窗體設計器生成的代碼 private void InitializeComponent() { //省略 } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } //運行腳本 private void buttonRun_Click(object sender, System.EventArgs e) { this.scriptEngine.Reset(); this.scriptEngine.Language = (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage),this.comboBoxScript.SelectedItem.ToString()); this.scriptEngine.Timeout = (int)this.numericUpDownTimeout.Value; this.scriptEngine.AllowUI = this.checkBoxAllowUI.Checked; if(this.radioButtonEval.Checked)//執行Eval方法 {this.textBoxResult.Text = this.scriptEngine.Eval(this.textBoxMethodName.Text+"("+this.textBoxParams.Text+")",this.textBoxCodeBody.Text).ToString(); } else//執行Run方法 { string[] parameters = (string[])this.textBoxParams.Text.Split(','); object [] paramArray = new object[parameters.Length]; for(int i = 0;i<parameters.Length;i++) paramArray[i] = Int32.Parse(parameters[i]); this.textBoxResult.Text = this.scriptEngine.Run(this.textBoxMethodName.Text,paramArray,this.textBoxCodeBody.Text).ToString(); } } //退出程序 private void buttonCancel_Click(object sender, System.EventArgs e) { this.Close(); } //錯誤函數 private void scriptEngine_RunError() { MessageBox.Show("RunError執行腳本錯誤!"); } private void scriptEngine_RunTimeout() { MessageBox.Show("RunTimeout執行腳本超時,引發錯誤!"); } } } 下面是測試程序運行界面: 在文本框中寫了一個JavaScript的函數。輸入12,輸出12000012。 如果把超時時間調整為1毫秒,那么執行該腳本就會跳出下面的超時提醒框,同時激發事件。 總結:上面演示了JavaScript腳本,如果有興趣讀者可以寫一些VBsript函數進行測試,腳本語言也只列出了三種,看了幫助,他還支持其他一些腳本,如果需要可以添加。另外,因為是調用Com,有些返回值是obejct類型的,需要進行轉換。在CSDN的技術論壇C#板塊下時常有朋友問這方面的問題,對于碰到這類問題的朋友,希望通過這篇文章能獲得一些你需要的幫助,很高興能和搞.net的朋友進行交流,我的郵件地址zhzuocn@163.com
|