如何:创建解决方案和项目生成配置
Visual Studio 自动化模型提供使您可以控制解决方案和项目生成配置的对象。解决方案生成配置指定如何生成和部署(如果已启用)解决方案中的某些项目。在**“项目属性页”**对话框中列出的项目生成配置列出了所有可用的项目生成类型(如调试或发布)和可用的平台(如 .Net 或 Win32)。对于每个生成和平台组合都有一个项目配置,即一组已定义的项目属性和设置。
解决方案生成配置对象是:
对象名 |
说明 |
---|---|
用于生成、清除和部署当前活动的解决方案配置。 |
|
表示要生成的项目及其配置的列表。 |
|
包含所有已定义的 SolutionConfiguration 对象。 |
|
表示 SolutionConfiguration 对象中的项目配置。 |
|
包含 SolutionConfiguration 对象中的所有 SolutionContext 对象。 |
|
表示在可以生成所属项目之前必须生成的项目。 |
|
包含在可以生成所属项目之前必须生成的所有项目。 |
项目生成配置对象是:
对象名 |
说明 |
---|---|
表示生成配置和平台。 |
|
表示指定平台中的配置或生成设置集。 |
|
包含所有 Configuration 对象。 |
|
OutputGroup 对象 |
包含由项目生成的文件。 |
OutputGroups 集合 |
包含所有 OutputGroup 对象。 |
使用这些对象,可以:
创建、将项目添加到、激活和删除解决方案配置。
生成、运行或部署解决方案配置中的任何项目。
获取有关项目中或解决方案配置中的对象的信息。
添加、移除或获取有关项目生成依赖项的信息。
说明 |
---|
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置。 |
示例
Public Sub OnConnection(ByVal application As Object, ByVal _
connectMode As ext_ConnectMode, ByVal addInInst As Object, _
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
_applicationObject = CType(application, DTE2)
_addInInstance = CType(addInInst, AddIn)
SConfig(_applicationObject)
End Sub
Sub SConfig(ByVal dte As DTE2)
Dim SB As SolutionBuild2 = _
CType(_applicationObject.Solution.SolutionBuild, _
SolutionBuild2)
Dim SolCtx As SolutionContext
Dim Proj As Project
Dim CM As ConfigurationManager
Dim Cfgs As SolutionConfigurations
Dim Cfg As SolutionConfiguration2
Dim msg As String
' Get a reference to the solution configurations
' solution context of the first project.
SolCtx = SB.SolutionConfigurations.Item(1).SolutionContexts.Item(1)
CM = _applicationObject.Solution.Projects. _
Item(1).ConfigurationManager
Proj = _applicationObject.Solution.Projects.Item(1)
Cfgs = _applicationObject.Solution.SolutionBuild. _
SolutionConfigurations
Cfg = CType(Cfgs.Item(1), SolutionConfiguration2)
' List the current solution build info.
msg = "BuildState = "
Select Case SB.BuildState
Case vsBuildState.vsBuildStateNotStarted
msg = msg & "Build has not yet started." & vbCr
Case vsBuildState.vsBuildStateInProgress
msg = msg & "Build is in progress." & vbCr
Case vsBuildState.vsBuildStateDone
msg = msg & "Build has completed." & vbCr
End Select
msg = msg & "Configuration Name = " & SolCtx.ConfigurationName _
& vbCr
msg = msg & "Platform Name = " & SolCtx.PlatformName & vbCr
msg = msg & "Project Name = " & SolCtx.ProjectName & vbCr
MsgBox(msg)
' List the current solution configurations.
msg = ("Configuration names are:" & vbCr)
For Each Cfg In Cfgs
msg = msg & Cfg.Name & vbCr
Next
MsgBox(msg)
' Add a new solution configuration.
Cfgs.Add("ANewConfiguration", "Debug", False)
MsgBox(Cfgs.Item(1).Name)
' Create a new project build configuration based on the Debug
' configuration.
Proj.ConfigurationManager.AddConfigurationRow("MyNewConfig", _
"Debug", True)
' Build the solution configuration.
sb.SolutionConfigurations.Item("MyConfig").Activate()
sb.Build()
End Sub
public void OnConnection(object application,
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
SConfig(_applicationObject);
}
public void SConfig(DTE2 dte)
{
try
{
SolutionBuild2 SB =
(SolutionBuild2)_applicationObject.Solution.SolutionBuild;
SolutionContext SolCtx;
Project Proj;
ConfigurationManager CM;
SolutionConfigurations Cfgs;
SolutionConfiguration2 Cfg;
String msg;
// Get a reference to the solution configurations
// solution context of the first project.
SolCtx = SB.SolutionConfigurations.Item(1).
SolutionContexts.Item(1);
CM = dte.Solution.Projects.Item(1).ConfigurationManager;
Proj = _applicationObject.Solution.Projects.Item(1);
Cfgs = _applicationObject.Solution.
SolutionBuild.SolutionConfigurations;
Cfg = (SolutionConfiguration2)Cfgs.Item(1);
// List the current solution build info.
msg = "BuildState = ";
switch (SB.BuildState)
{
case vsBuildState.vsBuildStateNotStarted:
msg = (msg + "Build has not yet started." + "\n");
break;
case vsBuildState.vsBuildStateInProgress:
msg = (msg + "Build is in progress." + "\n");
break;
case vsBuildState.vsBuildStateDone:
msg = (msg + "Build has completed." + "\n");
break;
}
msg = msg + "Configuration Name = " +
SolCtx.ConfigurationName + "\n";
msg = msg + "Platform Name = " + SolCtx.PlatformName + "\n";
msg = msg + "Project Name = " + SolCtx.ProjectName + "\n";
MessageBox.Show(msg);
// List the current solution configurations.
msg = "Configuration names are:" + "\n";
foreach(SolutionConfiguration2 tempConfigs in Cfgs)
{
msg = msg + tempConfigs.Name + "\n";
}
MessageBox.Show(msg);
// Add a new solution configuration.
Cfgs.Add("ANewConfiguration", "Debug", false);
MessageBox.Show
("The name of the first solution configuration item is: "
+ Cfgs.Item(1).Name);
// Create a new project build configuration based on the
// Debug configuration.
Proj.ConfigurationManager.AddConfigurationRow
("MyNewConfig", "Debug", true);
// Build the debug solution configuration.
MessageBox.Show("Build the solution in the debug mode...");
SB.SolutionConfigurations.Item("Debug").Activate();
SB.Build(true);
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex);
}
}
编译代码
若要编译此代码,请创建一个新的 Visual Studio 外接程序项目,并用该示例中的代码替换 Connect.cs 或 Connect.vb 类的代码。运行此外接程序前,打开 Visual Studio IDE 中的一个项目。有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序。