惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - 什么都不知道

DataSet的Xml序列化问题 VB.NET的一个小问题 Access is denied的问题 - 什么都不知道 存储过程output参数问题 SQL Server的效率? datagrid刷新问题 使用Visio DrawingControl的应用开发(补) 突起效果的Label WinForm下TextBox的数据绑定和更新 使用Radio按钮选择DataGrid行 如何在运行时加载不处于应用程序目录下的assembly 删除所有Windows组件 在ASP.NET中嵌入wml标记 c#中动态装载dll 处理大型xml文件 RedirectToMobilePage的问题 使用Visio 2003 Drawing Control开发应用(3)(4) 使用Visio 2003 Drawing Control开发应用(2) 使用Visio 2003 Drawing Control开发应用(1)
使用VSA给程序加上脚本支持
什么都不知道 · 2004-09-10 · via 博客园 - 什么都不知道

今天翻看.NET的类库,发现一个不知道的命名空间Microsoft.Vsa,于是找了找资料,才知道这个东西是为了给程序添加脚本支持。(当前只支持VB.NET,和JScript.NET),于是,试了试。
先要做一个引擎宿主,目的是把VSA(Visual Studio for Applicaiton)引擎调进来,然后执行脚本。

using System;
using Microsoft.Vsa;
using Microsoft.VisualBasic.Vsa;

namespace VsaEngine
{

    
public class ScriptHost : Microsoft.Vsa.IVsaSite
    
{
        
private byte[] code = new byte[1024];

        
private Microsoft.Vsa.IVsaEngine engine = null;
        
private Microsoft.Vsa.IVsaItems items = null;

        
public ScriptHost() //  设置vsa引擎的一些参数
        {
            engine 
= new Microsoft.VisualBasic.Vsa.VsaEngine();
            engine.RootMoniker 
= "test://cnblogs.com/VsaEngine";
            engine.Name 
= "VsaEngine";
            engine.RootNamespace 
= "VsaEngine";
            engine.Site 
= this;
            items 
= engine.Items;
            IVsaReferenceItem refItem 
= items.CreateItem("VsaEngine", Microsoft.Vsa.VsaItemType.Reference, VsaItemFlag.None) as IVsaReferenceItem;
            refItem.AssemblyName 
= System.IO.Directory.GetCurrentDirectory() + "\VsaEngine.dll";
            IVsaGlobalItem globalItem 
= items.CreateItem("HostObject", VsaItemType.AppGlobal, VsaItemFlag.None) as IVsaGlobalItem;
            globalItem.TypeString 
= "VsaEngine.ScriptHost";
        }


        
public bool SetScript(string scriptString) // 设置脚本,并进行语法检查
        {
            IVsaCodeItem codeItem 
= items.CreateItem("testCode", Microsoft.Vsa.VsaItemType.Code, Microsoft.Vsa.VsaItemFlag.Class) as IVsaCodeItem;
            codeItem.SourceText 
= scriptString;
            
return engine.Compile();
        }


        
public void Run() // 执行脚本
        {
            engine.Run();
        }


        
private string _testMember = "Test";
        
public string TestMember // 用来测试的一个属性
        {
            
get
            
{
                
return _testMember;
            }

            
set
            
{
                _testMember 
= value;
            }

        }


        
public string GetResult() // 获取脚本执行结果
        {
            System.Type type 
= engine.Assembly.GetType("VsaEngine.myModule"truetrue);
            System.Reflection.MethodInfo[] mi 
= type.GetMethods();
            System.Collections.IEnumerator enu 
= mi.GetEnumerator();
            System.Reflection.MethodInfo method 
= type.GetMethod("myMethod");
            
object result = method.Invoke(thisnull);

            
return result.ToString();
        }


        
IVsaSite 成员
    }

}


然后就可以调用了

        private void button1_Click(object sender, System.EventArgs e)
        
{
            System.Text.StringBuilder sbScript 
= new System.Text.StringBuilder();
            sbScript.Append(
"Module myModule ");
            sbScript.Append(
"Public Function myMethod As String ");
            sbScript.Append(
"With HostObject ");
            sbScript.Append(
".TestMember="Hello World!" ");
            sbScript.Append(
"End With ");
            sbScript.Append(
"Return " OK" ");
            sbScript.Append(
"End Function ");
            sbScript.Append(
"End Module ");

            ScriptHost host 
= new ScriptHost();
            
if(host.SetScript(sbScript.ToString()))
            
{
                host.Run();

                MessageBox.Show(
"VB script return value: " + host.GetResult() + " Local member value: " + host.TestMember);
            }

            
else
                MessageBox.Show(
"Compile Error");
        }


还可以把VSA的IDE一起做进来,我还没做测试程序。
总的感觉还不错。除了给程序增加脚本支持外,还可以模拟BizTalk做工作流引擎的组件处理了