搜索文章:

首页  |  Java技术  |  Asp.net  |  Asp编程  |  VC/C++  |  Delphi  |  VB编程

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

 }

         /// <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

()

相关文章:
© 2006   www.java-asp.net