在.NET中使用脚本引擎

来源:互联网 发布:深圳淘宝模特招聘 编辑:程序博客网 时间:2024/05/16 12:07
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
在应用程序中使脚本引擎可以大大增强程序的灵活性和可扩展性,类似MS Office和 VS.NET的宏功能。微软.NET框架中Microsoft.vsa.dll中定义了脚本引擎的框架,而Microsoft.Visualbase.vsa.dll则定义了实现了VBA的脚本引擎。利用这两个DLL可以定义自己的基于VBA.NET脚本引擎

Microsoft.vsa.dll中定义了一些框架接口,其中比较重要的接口包括
IVsaEngine 为脚本引擎接口,所有的脚本引擎使用该接口
IVsaSite 脚本引擎通讯接口,脚本引擎接受该接口来发布一些信息
IVsaItem 为脚本引擎相关资源的项目
IVsaCodeItem 为脚本代码模块
IVsaRefrenceItem 为脚本引擎使用的外部引用对象
IVsaGlobalItem 为脚本引擎使用的全局对象模块

使用过程一般为实例化一个脚本引擎,将实现了IVsaSite的对象传给脚本引擎对象,然后使用引擎的Items的CreateItem方法添加引用,代码文本和全局对象.然后调用Compile方法编译脚本引擎,使用RUN启动引擎。

脚本引擎启动后会在当前应用程序域中根据脚本代码动态生成和加载了一个Assembly程序集,使用 IVsaEngine.Assembly可访问动态产生的程序集。

Microsoft.VisualBase.vsa.dll实现了这些接口,并实现了基于VBA.NET语法的脚本引擎
主要对象为 VsaEngine

本程序提供一个C#写的演示程序,使用微软.NET框架1.1,该演示程序提供了一个通用的使用脚本引擎的模块,大家使用这个模块就可以非常方便的使用VBA脚本引擎来扩展自己的应用程序。

演示程序下载地址:http://www.cnblogs.com/Files/xdesigner/VBAScriptDemo.rar

出处:XDesigner BLOG

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>