using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Tools.Applications;
using Microsoft.VisualStudio.Tools.Applications.Contract;
using System.AddIn.Contract.Automation;
using System.Reflection;
namespace VSTASample
{
///
/// 运行态操作类
///
internal class RunManagement
{
private VSTAApplication application;
private Context macroContext;
private AddInCollection addInCollection;
private IHostItemProviderContract itemProvider;
private TypeInfrastructureManager typeInfrastructureManager;
private IEntryPointContract[] hostItems;
private string hostID;
private string templateName;
private IRemoteObjectContract remoteObjectContract;
private RemoteObject remoteObject;

///
/// 初始化RunManagement类的新实例。
///
internal RunManagement()
{
InitializeTypeInfrastructureManager();
}
internal void Connect( VSTAApplication application, string hostID, string templateName )
{
this.application = application;
this.application.RunManagement = this;
this.hostID = hostID;
this.templateName = templateName;
this.itemProvider = new HostItemProvider(application, TypeInfrastructureManager);
}
运行态 Managerment
Internal Properties
private void InitializeTypeInfrastructureManager()
{
if(typeInfrastructureManager == null)
{
typeInfrastructureManager = new TypeInfrastructureManager();
// This was auto-generated from ProxyGen with the /h:hostmapfile commandline argument.
global::System.Type hostType;
global::System.Type proxyType;

hostType = typeof(global::VSTASample.VSTAApplication);
proxyType = typeof(NonProxiableType<global::VSTASample.VSTAApplication>);
typeInfrastructureManager.CanonicalNameToTypeMap.Add("VSTASample, VSTASample.VSTAApplication", proxyType);
typeInfrastructureManager.TypeToCanonicalNa垃圾广告p.Add(hostType, "VSTASample, VSTASample.VSTAApplication");
}
}
}
}
///
/// 加载脚本
///
/// 脚本dll所在目录(如:C:\ShapeAppSamples\MyVSTADLLTest\Macros\bin)
/// 脚本dll名称(*.dll)
///
/// private VSTAHelper vstaHelper = new VSTAHelper();
/// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
///
public void LoadAddIns( string macroFilePath, string macroFileName )
{
app.RunManagement.LoadAddIns(macroFilePath, macroFileName);
}
///
/// 卸载脚本
///
///
/// private VSTAHelper vstaHelper = new VSTAHelper();
/// vstaHelper.UnloadAddIns();
///
public void UnloadAddIns()
{
app.RunManagement.UnloadAddIns();
}
///
/// 执行VSTA IDE中的方法
///
/// 需执行的方法名
///
///
/// private VSTAHelper vstaHelper = new VSTAHelper();
/// vstaHelper.LoadAddIns(@"..\bin","Demo.dll");
/// vstaHelper.ExecuteMacro("btn1_Click")
///
///
public void ExecuteMacro( string macroName )
{
app.RunManagement.ExecuteMacro(macroName);
}
private RunManagement runManagement;
///
/// 操作方法类
///
public RunManagement RunManagement
{
get
{
if(this.runManagement == null)
{
runManagement = new RunManagement();
runManagement.Connect(this, "VSTAHelper", "dlladdin.zip");
}
return runManagement;
}
set
{
runManagement = value;
}
}
VSTASample.VSTAHelper.Instance.LoadAddIns(@"C:\ShapeAppSamples\VSTASample\VSTASample\bin\macro\bin", "Sample.dll");
VSTASample.VSTAHelper.Instance.ExecuteMacro("fun1");
Public Function fun1()
MsgBox("HelloWorld!")
End Function来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-612009/ ,如需转载,请注明出处,否则将追究法律责任。