在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
()
- · MySQL数据导入导出方法与工具介绍(1)
- · 轻松弹出无边框网页的Javscrpt代码
- · ASP初学者参考
- · Java新手学习:IIS6和Tomcat5的整合
- · asp常用函数
- · W3 Jmail中文使用说明
- · 无组件上传图片到数据库中,最完整解决方案
- · 二级域名原理以及程序,申请即可开通
- · Java学习过程的一些重点
- · Oracle9i中如何建立不同字符集的数据库
- · 修复SQL Server 2000数据库之实战经验
- · 使用ASP生成HTML文件
- · 编写安全的ASP代码
- · 十天学会ASP.net(2)
- · 十天学会ASP.net(1)
- · 十天学会php(2)
- · 十天学会php(1)
- · 三天学好ADO
- · 在数据库中存取文件
- · 解决大字段在Form中Post出错的方法
- · 利用ASP+JMAIL进行邮件群发的新思路
- · 关于处理GET方式提交的含有特殊字符的参数
- · 关于Adodb.Stream 的使用说明
- · 多文件多文本框上传程序
- · 让妙客家标准版使用 SQL Server 数据库后台
- · 关于用SQL SERVER2000建立分布式网站系统的认识
- · 使用SQLSERVER的扩展存储过程实现远程备份与恢复
- · MySQL数据库类的定义
- · SQL语句导入导出大全
- · SQL Server的用户及权限
- · 深入理解C语言指针的奥秘
- · VC快捷键大全
- · 简体中文编码对应器
- · WEB打印大全
- · 用ASP实现远程批量文件改名
- · 想深入学习SQL injection的人这个可要好好看看
- · 用PHP创建PDF中文文档
- · PHP生成带有雪花背景的验证码
