前面四章是介绍,我们已经可以成功的通过一个winform程序打开一个VSTA的IDE编程窗口进行二次开发,但是对于我们使用来说是远远不够的,对于二次开发来说,最重要的一点是从主程序调用二次开发时编写的方法。
这一篇,我就简单介绍下如何调用VSTAIDE中编写的方法。
这里调用方法,第一步就是加载VSTA IDE中编写的代码编译而成的程序集(dll文件)到Microsoft.VisualStudio.Tools.Applications.AddIn和Contex中,第二步将准备调用的方法名称传给MethodInfo,通过反射调用实际方法,第三步不再需要使用的时候卸载程序集。
下面用代码进行讲解。
首先在VSTASAMPLE工程里添加一个RunManagement.cs,代码如下:
在VSTAHelper.cs的VSTAHelper类中,添加如下代码:这一篇,我就简单介绍下如何调用VSTAIDE中编写的方法。
这里调用方法,第一步就是加载VSTA IDE中编写的代码编译而成的程序集(dll文件)到Microsoft.VisualStudio.Tools.Applications.AddIn和Contex中,第二步将准备调用的方法名称传给MethodInfo,通过反射调用实际方法,第三步不再需要使用的时候卸载程序集。
下面用代码进行讲解。
首先在VSTASAMPLE工程里添加一个RunManagement.cs,代码如下:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Microsoft.VisualStudio.Tools.Applications;
5using Microsoft.VisualStudio.Tools.Applications.Contract;
6using System.AddIn.Contract.Automation;
7using System.Reflection;
8
9namespace VSTASample
10{
11 ///
12 /// 运行态操作类
13 ///
14 internal class RunManagement
15 {
16 private VSTAApplication application;
17 private Context macroContext;
18 private AddInCollection addInCollection;
19 private IHostItemProviderContract itemProvider;
20 private TypeInfrastructureManager typeInfrastructureManager;
21 private IEntryPointContract[] hostItems;
22 private string hostID;
23 private string templateName;
24
25 private IRemoteObjectContract remoteObjectContract;
26 private RemoteObject remoteObject;
27
28
29 ///
30 /// 初始化RunManagement类的新实例。
31 ///
32 internal RunManagement()
33 {
34 InitializeTypeInfrastructureManager();
35 }
36
37 internal void Connect( VSTAApplication application, string hostID, string templateName )
38 {
39 this.application = application;
40 this.application.RunManagement = this;
41 this.hostID = hostID;
42 this.templateName = templateName;
43
44 this.itemProvider = new HostItemProvider(application, TypeInfrastructureManager);
45 }
46
47 运行态 Managerment
154
155 Internal Properties
165
166 private void InitializeTypeInfrastructureManager()
167 {
168 if(typeInfrastructureManager == null)
169 {
170 typeInfrastructureManager = new TypeInfrastructureManager();
171
172 // This was auto-generated from ProxyGen with the /h:hostmapfile commandline argument.
173
174 global::System.Type hostType;
175 global::System.Type proxyType;
176
177
178 hostType = typeof(global::VSTASample.VSTAApplication);
179 proxyType = typeof(NonProxiableType<global::VSTASample.VSTAApplication>);
180 typeInfrastructureManager.CanonicalNameToTypeMap.Add("VSTASample, VSTASample.VSTAApplication", proxyType);
181 typeInfrastructureManager.TypeToCanonicalNa垃圾广告p.Add(hostType, "VSTASample, VSTASample.VSTAApplication");
182 }
183 }
184 }
185}
2using System.Collections.Generic;
3using System.Text;
4using Microsoft.VisualStudio.Tools.Applications;
5using Microsoft.VisualStudio.Tools.Applications.Contract;
6using System.AddIn.Contract.Automation;
7using System.Reflection;
8
9namespace VSTASample
10{
11 ///
12 /// 运行态操作类
13 ///
14 internal class RunManagement
15 {
16 private VSTAApplication application;
17 private Context macroContext;
18 private AddInCollection addInCollection;
19 private IHostItemProviderContract itemProvider;
20 private TypeInfrastructureManager typeInfrastructureManager;
21 private IEntryPointContract[] hostItems;
22 private string hostID;
23 private string templateName;
24
25 private IRemoteObjectContract remoteObjectContract;
26 private RemoteObject remoteObject;
27
28
29 ///
30 /// 初始化RunManagement类的新实例。
31 ///
32 internal RunManagement()
33 {
34 InitializeTypeInfrastructureManager();
35 }
36
37 internal void Connect( VSTAApplication application, string hostID, string templateName )
38 {
39 this.application = application;
40 this.application.RunManagement = this;
41 this.hostID = hostID;
42 this.templateName = templateName;
43
44 this.itemProvider = new HostItemProvider(application, TypeInfrastructureManager);
45 }
46
47 运行态 Managerment
154
155 Internal Properties
165
166 private void InitializeTypeInfrastructureManager()
167 {
168 if(typeInfrastructureManager == null)
169 {
170 typeInfrastructureManager = new TypeInfrastructureManager();
171
172 // This was auto-generated from ProxyGen with the /h:hostmapfile commandline argument.
173
174 global::System.Type hostType;
175 global::System.Type proxyType;
176
177
178 hostType = typeof(global::VSTASample.VSTAApplication);
179 proxyType = typeof(NonProxiableType<global::VSTASample.VSTAApplication>);
180 typeInfrastructureManager.CanonicalNameToTypeMap.Add("VSTASample, VSTASample.VSTAApplication", proxyType);
181 typeInfrastructureManager.TypeToCanonicalNa垃圾广告p.Add(hostType, "VSTASample, VSTASample.VSTAApplication");
182 }
183 }
184 }
185}
1///
2 /// 加载脚本
3 ///
4 /// 脚本dll所在目录(如:C:\ShapeAppSamples\MyVSTADLLTest\Macros\bin)
5 /// 脚本dll名称(*.dll)
6 ///
7 /// private VSTAHelper vstaHelper = new VSTAHelper();
8 /// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
9 ///
10 public void LoadAddIns( string macroFilePath, string macroFileName )
11 {
12 app.RunManagement.LoadAddIns(macroFilePath, macroFileName);
13 }
14
15 ///
16 /// 卸载脚本
17 ///
18 ///
19 /// private VSTAHelper vstaHelper = new VSTAHelper();
20 /// vstaHelper.UnloadAddIns();
21 ///
22 public void UnloadAddIns()
23 {
24 app.RunManagement.UnloadAddIns();
25 }
26
27 ///
28 /// 执行VSTA IDE中的方法
29 ///
30 /// 需执行的方法名
31 ///
32 ///
33 /// private VSTAHelper vstaHelper = new VSTAHelper();
34 /// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
35 /// vstaHelper.ExecuteMacro("btn1_Click")
36 ///
37 ///
38 public void ExecuteMacro( string macroName )
39 {
40 app.RunManagement.ExecuteMacro(macroName);
41 }
继续在VSTAApplication类中添加如下代码:2 /// 加载脚本
3 ///
4 /// 脚本dll所在目录(如:C:\ShapeAppSamples\MyVSTADLLTest\Macros\bin)
5 /// 脚本dll名称(*.dll)
6 ///
7 /// private VSTAHelper vstaHelper = new VSTAHelper();
8 /// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
9 ///
10 public void LoadAddIns( string macroFilePath, string macroFileName )
11 {
12 app.RunManagement.LoadAddIns(macroFilePath, macroFileName);
13 }
14
15 ///
16 /// 卸载脚本
17 ///
18 ///
19 /// private VSTAHelper vstaHelper = new VSTAHelper();
20 /// vstaHelper.UnloadAddIns();
21 ///
22 public void UnloadAddIns()
23 {
24 app.RunManagement.UnloadAddIns();
25 }
26
27 ///
28 /// 执行VSTA IDE中的方法
29 ///
30 /// 需执行的方法名
31 ///
32 ///
33 /// private VSTAHelper vstaHelper = new VSTAHelper();
34 /// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
35 /// vstaHelper.ExecuteMacro("btn1_Click")
36 ///
37 ///
38 public void ExecuteMacro( string macroName )
39 {
40 app.RunManagement.ExecuteMacro(macroName);
41 }
1private RunManagement runManagement;
2 ///
3 /// 操作方法类
4 ///
5 public RunManagement RunManagement
6 {
7 get
8 {
9 if(this.runManagement == null)
10 {
11 runManagement = new RunManagement();
12 runManagement.Connect(this, "VSTAHelper", "dlladdin.zip");
13 }
14 return runManagement;
15 }
16 set
17 {
18 runManagement = value;
19 }
20 }
在Form1窗体中添加一个按钮,并添加click事件,代码如下:2 ///
3 /// 操作方法类
4 ///
5 public RunManagement RunManagement
6 {
7 get
8 {
9 if(this.runManagement == null)
10 {
11 runManagement = new RunManagement();
12 runManagement.Connect(this, "VSTAHelper", "dlladdin.zip");
13 }
14 return runManagement;
15 }
16 set
17 {
18 runManagement = value;
19 }
20 }
1VSTASample.VSTAHelper.Instance.LoadAddIns(@"C:\ShapeAppSamples\VSTASample\VSTASample\bin\macro\bin", "Sample.dll");
2VSTASample.VSTAHelper.Instance.ExecuteMacro("fun1");
运行程序,打开VSTA IDE,在Helper.vb中添加代码:2VSTASample.VSTAHelper.Instance.ExecuteMacro("fun1");
1Public Function fun1()
2 MsgBox("HelloWorld!")
3End Function
编译后,在主程序窗体中点击新增的按钮,调用VB程序,弹出对话框,内容为“HelloWorld!”2 MsgBox("HelloWorld!")
3End Function