Keywords: Visualstudio Template VSDIR VIZ
Objective: To create your own Visual Studio template to create a class that implements single-size mode (Singleton) as an example.
1, what is the template?
It can be briefly described in two examples.
When we choose to create a new project in Visual Studio.NET, the wizard usually provides some predefined types of items, such as "Window Apps", "ASP.NET Web Applications". As long as you select one of them and determine, VS will automatically automatically the project structure and part of it thinks useful code. These optional stuff is a template, a project template.
When we want to add some files in the project, you can usually use the "Add New Item". For example, add a new class or an XML file, and the like. At this time, the type we have chosen is also one of the VS predefined template, called item template (Item Template).
2, why do you want to use a template?
The template focuses on development efficiency, that is, through these templates we can automatically generate some common files, and initialize the structure and code to speed up the development speed.
Typical is those templates (such as component classes, user controls, etc.) when "add new items" in VS.
This POST focuses on the creation and application of the item template. The way to create a project template is similar to this.
http://www.carlosag.net/articles/createvstemplate.aspx.
3, what is the use template?
Since you want to create your own template, you should clear what you want to make this template. Since the previous is mentioned, the item template that creates a class that automatically implements single example mode (Singleton). (BTW: About the Singleton mode itself can be found:
Http://www.yoda.arachsys.com/csharp/singleton.html. About it discussed, here there is one
http://www.jdon.com/jive/Article.jsp?forum=91&thread=17578. )
Here first gives an example of the simplest SINGLETON mode:
Public Sealed Class Singleton
{
Static Singleton Instance = NULL;
Singleton ()
{
}
Public Static Singleton Instance
{
get
{
IF (instance == null)
{
Instance = new singleleton ();
}
Return Instance;
}
}
}
4, how to create an item template?
Create a project template to complete the following steps:
A. Creating an item template's VSDIR file [VSDIR file description can be found
http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vsintro7/html/vxconvsdirfiles.asp]
b, create a vsz file (project control) [Description of VSZ files can be found
Http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vcore/html/vclrfunderstandingProjectControlFiles.asp]
C, custom JavaScripts
D, create a file template
Next, it will be expanded one by one.
Create a vsdir file
-------------------------------------------------- ------------------------------- Open folder
Open the code.vsdir file and add it below:
../../Csharpaddsingletonclasswiz.vsz| {FAE04EC1-301F1D3-BF4B-00C04F79EFBC }| | {FAE04EC1-301F-11D3-BF4B-00c04F79EFBC} | 4515 | 0 | Singletonclass.cs
* Note:
Contains not to wrap. This file format is defined as a VSDir file description.
Where CSHARPADSINGETONCLASSWIZ.VSZ refers to the location of the VSZ file (project control) used.
As shown in the drawings, the Singleton class template will appear under the "code" branch of the Local Project Item after adding this. If you need to display this "Singleton Class" template under Local Project Items, you need to add: on the
Create a vsz file
-------------------------------------------------- ------------------------------
- Open folder
Create a file csharpaddsingletonclasswiz.vsz, add the following:
Vswizard 7.0
Wizard = vswizard.vswizarden.7.1
Param = "wizard_name = csharpaddsingletonclasswiz"
Param = "wizard_ui = false"
PARAM = "Project_type = csproj"
* Note: VSZ file format definition See vsz file description
Among them, it is important that param = "wizard_name = csharpaddsingletonclasswiz", "CsharPadDsingletonclasswiz" is a wizard name that is used when creating a Singleton class.
Custom JavaScripts
-------------------------------------------------- ------------------------------
Open Directory
Create a directory called CSHARPADSINGLETONCLASSWIZ, this name is corresponding to param = "wizard_name = csharpaddsingletonclasswiz" Wizard_name. Open this directory, create a directory scripts, and then create a directory 1033 under Scripts. Then create a default.js file. Place the following code:
Function ONFINISH (Selproj, Selobj)
{
Var oldsuppressuivalue = true;
Try
{
Var stratarget = wizard.findsymbol ("item_name");
Var strclassname = stratageget.split (".");
Var bvalid = wizard.validateclridentifier (STRCLASSNAME [0]);
IF (! BValid)
{
Wizard.reporterror ();
Return vs_e_wizardbackbuttonpress;
}
Oldsuppressuivalue = DTE.SUPPRESSUI;
Var strprojectname = wizard.findsymbol ("Project_name");
Var strsafeProjectname = createsafename (strProjectName);
Wizard.Addsymbol ("Safe_Project_Name", StrsafeProjectName);
SetTargetFullPath (SELOBJ);
Var strprojectpath = wizard.findsymbol ("Target_FullPath");
Var startemplatepath = wizard.findsymbol ("templates_path");
Var strTPL = "";
Var strname = "";
VAR infile = createinffile ();
AddReference (Selproj);
AddFileStocsharpproject (Selobj, StrojectName, StrProjectPath, Infile, True);
}
Catch (e)
{
IF (E.DESCRIPTION.LENGTH> 0)
SETERRORINFO (E);
Return E.NUMBER;
}
Finally
{
DTE.SUPPRESSUI = OldsuppRessuivalue;
IF (Inffile)
Inffile.delete ();
}
}
Function SetFileProperties (ofileIleItem, StrfileName)
{
}
* Note: Methods appearing in this JS file can be found in the path
Create a file template
-------------------------------------------------- ------------------------------
Go to the path:
{
Using system;
/ **
/// [! Output Safe_Class_name].
/// summary>
Class [! outprut safe_class_name]
{
Member #Region member
Private static [! output safe_class_name] m_instance;
#ndregion
Constructor #REGION constructor
Private [! outprut safe_class_name] ()
{
//
// TODO: Add constructor logic here
//
}
Public static [! output safe_class_name] getInstance ()
{
IF (m_instance == null)
{
m_instance = new [! output safe_class_name] ();
}
Retrun m_INSTANCE;
}
#ndregion
}
}
All right. The customization of an item template is completed through the above steps.
When we open a project, when you choose Add new item, you will find a template for a "Singleton class" in the local project item. Figure:
The "Singleton Class" template can also be seen in the "code" of the Local Project Items.
When you are selected and view the code, you can find that the created class implements the Singleton mode described above.
OK. At this point, how to define item templates is basically clear. In this way, we can try to customize the collection of item templates, such as implementing some of the commonly used patterns into item templates, which will undoubtedly help increase the development efficiency!