文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页精文荟萃软件资讯 → 在C#中调用VBScript等脚本的实现(下)

在C#中调用VBScript等脚本的实现(下)

时间:2004/10/7 18:28:00来源:本站整理作者:蓝点我要评论(0)


            
             
              
             
            

               
               

            



             }

         ///


         /// 获取或设置脚本语言


         ///


         public ScriptLanguage Language


         {


              get{return (ScriptLanguage)Enum.Parse(typeof(ScriptLanguage),this.msc.Language,false);}


              set{this.msc.Language = value.ToString();}


         }


         ///


         /// 获取或设置脚本执行时间,单位为毫秒


         ///


         public int Timeout


         {


              get{return this.msc.Timeout;}


              set{this.msc.Timeout = value;}


         }


         ///


         /// 设置是否显示用户界面元素


         ///


         public bool AllowUI


         {


              get{return this.msc.AllowUI;}


              set{this.msc.AllowUI = value;}


         }


         ///


         /// 宿主应用程序是否有保密性要求


         ///


         public bool UseSafeSubset


         {


              get{return this.msc.UseSafeSubset;}


              set{this.msc.UseSafeSubset = true;}


}


         ///


         /// RunError事件激发


         ///


         private void OnError()


         {


              if(RunError!=null)


                   RunError();


         }


         ///


         /// OnTimeout事件激发


         ///


         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

                       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

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3

最新文章 微信3.6.0测试版更新了微信支付漏洞会造成哪 360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本