Project template parameterization (I)

Source: Internet
Author: User

If we want the previous project template to be more practical, for exampleCodeAdd copyright information, including the project name and creation time specified at the time of creation. What should I do? In fact, Visual Studio project templates have such a mechanism. I personally define it as a "parameterized" mechanism.

Let's take a look at a picture first, which is very simple.

As you can see, the "parameterization" defined here refers to a dynamic process, including the "parameter formatting" in the preparation phase of the Project template ", and the additional action "parameter replacement" during template instantiation ".

Parameter formatting: in the preparation phase (that is, to create a "Project template source"), we can add some parameter placeholders to any source file. The placeholder format is $ parameter name $, parameter names are case-sensitive. Visual Studio's own template mechanism only recognizes parameters in this format.

Parameter replacement: parameter replacement is the execution phase of parameterization.ArticleI have summarized that Visual Studio templates have three parameter replacement methods, which can make template instantiation a reality.

Method 1: replace built-in parameters retained by the System

Visual Studio has some built-in parameters for us to use. As long as the placeholder parameters defined in the source file are consistent with the built-in parameter names, IDE will automatically replace the parameters, the following table lists the built-in parameters, from msdn:

 

The parameters file, put these built-in parameters in it, and let the IDE help us with the replacement. Note that the itemname, safeitemname, and rootnamespace must be excluded here, because they are used for the item template.

At the same time, WebClient. vstemplate uses the projectitem node to add this new source file and replaceparameters is set to true. This tells Visual Studio that we want to retrieve the parameters in this source file and let Visual Studio help us replace the parameter values.

 
<Projectitem replaceparameters ="True"> Buildinparameterexperiment.txt </projectitem>

Finally, package and deploy the template to create a project. The result is:

Limitations of this method: because it is built-in, it is only limited to some of the simplest and common parameters.

Method 2: Custom parameters and transmission

If the defined parameter is not built-in, how can we pass the value. The Visual Studio template provides the ability to pass custom parameter values.

First, we create a new customparameterexperiment.txt file and define two parameters. The names here are selected by ourselves:
CommonName = $ CommonName $
Applicationname = $ applicationname $
 

Well, what we need to do is to tell Visual Studio the values of these parameters so that Visual Studio can replace our custom parameters with these values in the template instantiation phase. How? Simply add the parameters to the customparameters set under the templatecontent node.

 
<Customparameters>
 
<Customparameter name ="$ Pipeline name $"Value ="Ethan woo"/>
 
<Customparameter name ="$ Applicationname $"Value ="Two Layer template parameter experiment application"/>
 
</Customparameters>

Re-package and deploy the service. Use this template to create a project instance. The result is as follows:

Limitations of this method: Obviously, the parameter value is written to an end. The actual requirement is to interact with the user, capture the user input, and then process it. Can this be done? The answer is yes. You must trust Microsoft!

In order to enhance ease of use, this method will be specifically described in the next article.

 

From: http://www.ethan-woo.com/post/2011/05/02/VisualStudio-Template-Parameterization-First.aspx

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.