Automatic Generation and configuration (copying) in Visual Studio. NET)

Source: Internet
Author: User

 

Automatic Generation and configuration in Visual Studio. NET

Abstract: This article describes how to use the Microsoft Visual Studio. NET Automation Model to automate solution generation, project generation, and configuration generation.

Directory

A solution and project automatically generate Model Objects

B control generation Configuration

 

B1) solution generation Configuration

 

B2) Project generation Configuration

 

C. Start solution and project generation

 

D. Project generation dependency

 

E. Define the startup project

 

F Summary

Introduction

When you generate a project or solution in Visual Studio. NET, you must generate the project or solution according to the configuration associated with it. Generally, you can use an integrated development environment (IDE) to manually generate a solution or project. net automation model allows you to change the configuration and complete the generation operation programmatically.

With model generation, you can select the project to be generated and exclude those that do not need to be generated. In addition, you can also use the generate configuration to determine how to generate the option. You can define two types of configuration generation in Visual Studio: solution and project. With the solutionbuild object, you can run, debug, and deploy a solution or selected project programmatically.

This feature is useful for creating unattended builds, automated test suites, and batch jobs to control generation configurations and generating projects and solutions programmatically. In addition, you can use the generated model to complete the following tasks:

· Create, activate, and delete solution configurations.

· Generate, run, or deploy any or all projects in the solution configuration.

· Obtain information about objects in the project or solution configuration.

· Add, delete, or obtain information about dependencies generated by a project.

Most of the solutions and project-generated objects in the Visual Studio. NET Automation Model correspond to commands in Visual Studio. net ide. For example, the solutionbuild object contains members equivalent to the commands in the build (generate) menu: Build solution (generate solution), rebuild solution (regenerate solution), build project (generate project) and rebuild project (re-build project.

For more information about how the solution and project generation configurations work, and how to set them in IDE, see builds during application development in the Visual Studio product documentation) and build deployments ).

Automatically generate model objects for solutions and projects

Visual Studio. net automatically generates some objects and sets in the model so that you can modify the configuration and dependencies of the solution and project, and start the generation through programming. These objects and sets include:

Object Name

Description

Builddependency object

The project that must be generated before the main project is generated.

Builddependencies set

Contains all projects that must be generated before the main project is generated.

Configuration object

Indicates project configuration or configuration set generation. For example, debug project configuration for the. NET platform.

Configurations set

Contains all project configuration objects.

Configurationmanager object

Generate the configuration and platform.

Outputgroup object

Contains files generated by the project.

Outputgroups set

Contains all outputgroup objects.

Solutionbuild object

Used to generate, clear, and deploy active solution configurations.

Solutionconfiguration object

The list of projects to be generated and their configurations.

Solutionconfigurations set

Contains all the defined solutionconfiguration objects.

Solutioncontext object

The generate context of each project in the solutionconfiguration object.

Solutioncontexts set

Contains all the solutioncontext objects in the solutionconfiguration object (each project corresponds to one ).

In the Visual Studio. NET Automation Model, the hierarchical structure of the relationship between objects and sets is as follows:

DTE

Solutionbuild

Builddependencies

Builddependency

Solutionexcepations

Solutionconfiguration

Solutioncontexts

Solutioncontext

Project

Configurationmanager

Deployments

Configuration

Outputgroups

Outputgroup

For detailed details about the complete Visual Studio. NET Automation Model, see Automation Object Model chart in Visual Studio product documentation ).

For more information about available configurations and project context, see configuration, configuration properties, and solution property pages dialog box ).

Control generation Configuration

There are two types of configuration generation: solution and project. The following sections describe how to create and control each type of configuration programmatically.

Solution generation Configuration

Solution generation configuration specifies how to generate specific projects in the solution and how to deploy these projects (if enabled ). The solution includes two default generation configurations: Debug and release ). In the "Configuration Manager" dialog box, you can create a solution configuration, delete a configuration, or edit an existing configuration. You can also use the solutionconfiguration object to perform the preceding operations programmatically. The following vsmacro example illustrates how to implement the above operations.

Sub solutionconfigurationexample ()

'Set the solution configuration to release (release ).

Dim solncfg as solutionconfiguration = DTE. solution ._

Solutionbuild. solutionexcepations. Item ("release ")

'Removing the comment mark in the next line will delete the currently selected configuration.

'Note: The last solution configuration cannot be deleted.

'Solncfg. Delete ()

'Create a new configuration based on the existing debug (debugging)

'Solution configuration. The new configuration must be based on the existing configuration.

DTE. solution. solutionbuild. solutionexcepations. Add ("newsolncfg ",_

"Debug", false)

'Activate the specified solution configuration. In this example

'Newsolncfg ".

Solncfg = DTE. solution. solutionbuild. solutionexcepations ._

Item ("newsolncfg ")

Solncfg. Activate ()

End sub

The solution context is displayed in the Configuration Manager dialog box of the solution, that is, project, project configuration, and platform) and whether to generate. The solutioncontext object indicates the attribute value of this row. The solutioncontexts set contains all rows configured for this solution.

The following example lists all solution context values for each project in the debug solution configuration.

Sub solnctx ()

'Select the debug solution configuration.

Dim solncfg as solutionconfiguration = DTE. solution ._

Solutionbuild. solutionexcepations. Item ("debug ")

Dim solnctx as solutioncontext

Dim solnctxs as solutioncontexts = solncfg. solutioncontexts

Dim MSG as string

'List the context values of each project in the solution.

For each solnctx in solnctxs

MSG = "Project name:" & solnctx. projectname & vbcr

MSG = MSG & "configuration name:" & solnctx. configurationname &_

Vbcr

MSG = MSG & "platform name:" & solnctx. platformname & vbcr

MSG = MSG & "whether to generate:" & solnctx. shouldbuild & vbcr

MSG = MSG & "deployment:" & solnctx. shoulddeploy & vbcr

Msgbox (MSG)

Next

End sub

Project generation Configuration

Project generation configuration includes multiple sets for debugging, generation, and deployment. These configuration settings are listed in the "Project property pages" dialog box. You can view these settings by right-clicking a project in Solution Explorer and selecting "properties. The available project configuration name and platform name are displayed in the drop-down list at the top of the dialog box.

The "common properties" and "configuration properties" nodes in the dialog box list the attributes of all available project generation configurations that you can change. For example, to view or change the generation attributes, click the "build" node under "configuration properties. After you change the settings and select "OK", the settings for the specific configuration name and platform name will be saved. With the objects in the Visual Studio. NET Automation Model, you can control the settings in the project generation configuration programmatically.

For more information about how configurations are generated and how to create them, see default and custom builds ).

The following vsmacro example shows how to change the value set in the project generation configuration.

Sub projconfig ()

'This example shows how to change

'Define the Boolean value set for the trail constant.

'This example requires a project to be loaded.

Dim proj as project = DTE. vbprojects. Item (1)

Msgbox ("Project name:" & proj. Name)

Msgbox ("the properties of the project configuration to be changed:" & proj ._

Configurationmanager. activeconfiguration. properties. Item (3). Name)

Msgbox ("Current Value:" & proj. configurationmanager ._

Activeconfiguration. properties. Item ("definetrace"). value)

Proj. configurationmanager. activeconfiguration. properties. Item _

("Definetrace"). value = true

Msgbox ("New Value:" & proj. configurationmanager ._

Activeconfiguration. properties. Item ("definetrace"). value)

End sub

Another way to control project configuration properties is to use the configurationmanager object. The following example shows how to perform this operation.

Sub branch mgrexample ()

'The example shows how to use configurationmanager.

'Object to set project configuration properties.

Dim proj as project = DTE. vbprojects. Item (1)

Dim MSG as string

MSG = "Project name:" & proj. Name & vbcr

MSG = MSG & "project property name:" & proj. configurationmanager ._

Item (1). properties. Item (1). Name & vbcr

MSG = MSG & "project property value:" & proj. configurationmanager ._

Item (1). properties. Item (1). Value & vbcr

Msgbox (MSG)

MSG = ""

Proj. configurationmanager. Item (1). properties. Item (1). value = false

MSG = "Project name:" & proj. Name & vbcr

MSG = MSG & "project property name:" & proj. configurationmanager ._

Item (1). properties. Item (1). Name & vbcr

MSG = MSG & "project property value:" & proj. configurationmanager ._

Item (1). properties. Item (1). Value & vbcr

Msgbox (MSG)

End sub

Start solution and project generation

With Visual Studio. NET automated model, you can program the solution and project generation. The main object to implement this function is the solutionbuild object. You can use this object:

· Generate a solution by calling the build method of the solutionbuild object.

· Generate a specific project in the solution by calling the buildproject method.

· Call the Debug Method to start the "debug" Generation of the solution.

· Deploy certain projects in the solution by calling the deploy method.

· Call the run method to run the specified startup project.

Various properties of the solutionbuild object allow you to access:

· Active configuration (that is, the current solution configuration that will be used when a solution is generated ).

· Generate dependencies (that is, which projects can be correctly generated only by other projects ).

· Generation status (that is, whether the generation has been started in the current environment session, whether the generation is in progress, or whether the generation has been completed ).

The solutionbuild object also contains the clean method, which can be called to delete the support files generated by the compiler from the project (marked as generated in the active solution configuration. In addition, the solutionbuild object has other attributes. For a complete list, see solutionbuild object properties, methods, and events ).

The following example shows how to start the generation of the current solution configuration. This example assumes that your generated configuration is named "mybuildconfig ".

Sub solutionbuildexample ()

'Generate the active solution configuration.

Dim Sb as solutionbuild = DTE. solution. solutionbuild

SB. solutionexcepations. Item ("mybuildconfig"). Activate

SB. Build

End sub

The following example shows how to start the generation of specific projects and their dependencies in the solution. This example assumes that your generated configuration is named "mybuildconfig" and the current solution contains a project named "leleapplication1.

Sub projectbuildexample ()

'Generate the project specified in the solution.

Dim Sb as solutionbuild = DTE. solution. solutionbuild

SB. buildproject ("mybuildconfig", "leleapplication1", false)

End sub

Note: The waitforbuildtofinish flag is used to determine whether the generation operation retains control until the generation operation is completed. The default value is false. If you set waitforbuildtofinish to false (that is, control to return immediately after the generation operation is started), you can use the builddone event to confirm that the generation is complete.

Project generation dependency

Visual Studio. Net also allows you to use the builddependencies attribute to define the dependency between two projects. The following example shows how to make project1 dependent on project2. In this way, the solution must be converted to project2.

Sub builddependenciesexample ()

'Project 1 depends on Project 2.

Dim P1 as project = DTE. solution. Item (1) 'Project 1

Dim P2 as project = DTE. solution. Item (2) 'Project 2

Dim Sb as solutionbuild = DTE. solution. solutionbuild

SB. builddependencies. Item (p1.uniquename). addproject (p2.uniquename)

End sub

Define startup project

Startup project defines the project to run when the Visual Studio debugger is started. You can use the startupprojects attribute of the solutionbuild object to define this setting programmatically.

Note: currently, only one startup project can be set for the startupprojects set in Visual Studio. NET, but multiple startup projects will be allowed in future versions.

The following example shows how to view and change the current startup project.

Sub startupproj ()

'This example requires that two projects exist in the solution.

Dim Sb as solutionbuild = DTE. solution. solutionbuild

Dim P1 as project = DTE. solution. Item (1) 'Project 1

Dim P2 as project = DTE. solution. Item (2) 'Project 2

Msgbox ("Current startup project:" & SB. startupprojects (0 ))

'Change the startup project to another project.

SB. startupprojects = p1.uniquename

Msgbox ("new startup project:" & SB. startupprojects (0 ))

End sub

Summary

Visual Studio. NET's automated generation model provides many options for controlling solutions, projects, and their generation and configuration through programming. To learn more about Visual Studio.. Net to automatically generate the relationship between the model and Ide. Use the Object Browser to browse the objects listed in "The solution and project automation build model objects, and compare their members with the dialog box. Practice using models to understand how they work. You can also see the "prepostbuildrules add-in" sample on the web site of the Visual Studio. NET automation example at http://msdn.microsoft.com/vstudio/nextgen/automation.asp ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.