All templates Support Parameter replacement during template instantiation to replace key parameters, such as class names and namespaces. When you click OK in the "New Project" and "Add new project" dialog boxes, these parameters will be replaced by the template wizard running in the background.
Declare and enable template parameters
Template parameters start with $Parameter$ Format. For example:
$ Safeprojectname $
$ Safeclassname $
$ Guid1 $
$ Guid5 $
Enable parameter substitution in the template
In the. vstemplate file of the template, locateProjectitemElement.
SetProjectitemElementReplaceparametersSet propertyTrue.
In the code file of the project item, the proper position in the Code includes parameters. For example, the following parameter specifies the security project name used for the namespace in the file:
Copy codenamespace $safeprojectname$
Reserved template parameters
The following table lists the reserved template parameters available for all templates.
Note: |
The template parameters are case sensitive. |
Parameters |
Description |
Clrversion |
The current version of the Common Language Runtime Library (CLR. |
Guid [1-10] |
The GUID used to replace the project guid in the project file. You can specify up to 10 unique guids (for example,Guid1)). |
Itemname |
The name provided by the user in the Add new project dialog box. |
Machinename |
The name of the current computer (for example, computer01 ). |
Projectname |
The name provided by the user in the new project dialog box. |
Registeredorganization |
The registry key value in HKLM \ Software \ Microsoft \ Windows NT \ CurrentVersion \ registeredorganization. |
Rootnamespace |
The root namespace of the current project. This parameter is used to replace the namespace in the items added to the forward project. |
Safeitemname |
The name provided by the user in the "Add new item" dialog box. All unsafe characters and spaces are removed from the name. |
Safeprojectname |
The name provided by the user in the "New Project" dialog box. All unsafe characters and spaces are removed from the name. |
Time |
The current time in the format of DD/MM/YYYY 00:00:00. |
Userdomain |
The current user domain. |
Username |
The current user name. |
Year |
The current year in yyyy format. |
Custom template parameters
In addition to the reserved template parameters that are automatically used during parameter replacement, you can also specify your own template parameters and values. For more information, see How to: pass custom parameters to a template.
Example: Replace the file name
You can useTargetfilenameThe property parameter specifies the variable file name for the project item. For example, you can specify the. exe file to use$ Projectname $The specified project name is used as the file name.
Copy code
<TemplateContent><ProjectItemReplaceParameters="true"TargetFileName="$projectname$.exe">File1.exe</ProjectItem>...</TemplateContent>
Example: Use the project name as the namespace name
To use the project name in the namespace of the Visual C # class file class1.cs, use the following syntax:
Copy code
#region Using directivesusing System;using System.Collections.Generic;using System.Text;#endregionnamespace $safeprojectname${public class Class1{public Class1(){}}}
In the. vstemplate file of the Project template, include the following XML When referencing the class1.cs file:
Copy code
<TemplateContent><ProjectItem ReplaceParameters="true">Class1.cs</ProjectItem>...</TemplateContent>