阿瑞空间

---------激情而不浮躁-------
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Visual Studio自定义模板(一)

Posted on 2008-10-06 20:48  阿瑞  阅读(1226)  评论(1编辑  收藏  举报

  每个公司都有自己的编码标准,其中最基本的一条就是文件要有文件头,但是Visual Studio中默认的模板是没有任何文件头信息的,这就需要我们定义自己的模板。

Visual Studio里的模板分为两类,项目模板和项模板,项目模板就是我们在添加项目是应用的模板,如Windows应用程序,类库等等,出现在“添加新项目”的对话框中,如下图,

 

添加新项目对话框

 

  我们在一个项目中添加新项时,比如添加一个类,一个接口,等等,这是应用的模板是项模板,出现在“添加新项”的对话框中,如下图,

  添加新项对话框

  这篇文章主要介绍如何自定义项模板。

  在Visual Studio中定义自己的模板有一个非常快捷的方式,先新建一个文件,定义我们想要的模板格式,再通过[文件]菜单下的[导出模板]子菜单生成一个模板,如下图,

  导出模板菜单

  下面是我定义的一个简单的模板,    

#region Copyright (C) Rainsoft All rights reserved
/*******************************************************************************************
* Creation:
* Author: $username$
* Date: $time$
* Description:
* Version:
* Modification:
* Author:
* Date:
* Description:
* Version:
******************************************************************************************
*/
#endregion

namespace $rootnamespace$
{
using System;

public class $itemname$
{
}
}

 


  通过[导出模板]->选择项模板,就可快速的生成一个自定义模板,生成完后,在“添加新项”的对话框中就会出现我们刚才定义的模板,如下图,

  我的自定义模板

  下面是通过该模板生成的一个类, 

#region Copyright (C) Rainsoft All rights reserved
/*******************************************************************************************
* Creation:
* Author: Arrui
* Date: 2008-10-6 19:33:51
* Description:
* Version:
* Modification:
* Author:
* Date:
* Description:
* Version:
******************************************************************************************
*/
#endregion

namespace MyTemplate
{
using System;

public class MyClassTemplate2
{
}
}

 



  在我们刚定义的模板文件中,用到了$rootnamespace$, $username$等一些模板参数,这些都是Visual Studio给我们提供的一些默认的模板参数,这些参数有

Parameter Description
clrversion Current version of the common language runtime (CLR).
GUID [1-10] A GUID used to replace the project GUID in a project file. You can specify up to 10 unique GUIDs (for example, guid1).
itemname The name provided by the user in the Add New Item dialog box.
machinename The current computer name (for example, Computer01).
projectname The name provided by the user in the New Project dialog box.
registeredorganization The registry key value from HKLM\Software\Microsoft\Windows NT\CurrentVersion\RegisteredOrganization.
rootnamespace The root namespace of the current project. This parameter is used to replace the namespace in an item being added to a project.
safeitemname The name provided by the user in the Add New Item dialog box, with all unsafe characters and spaces removed.
safeprojectname The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed.
time The current time in the format DD/MM/YYYY 00:00:00.
userdomain The current user domain.
username The current user name.
webnamespace The name of the current Web site. This parameter is used in the Web form template to guarantee unique class names. If the Web site is at the root directory of the Web server, this template parameter resolves to the root directory of the Web Server.
year The current year in the format YYYY.

  (from msdn: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vssoln/html/1b567143-08c6-4d7a-b484-49f0671754fe.htm)
  注意,这些模板参数都是大小定敏感的。
  一般来说,通过这些模板参数足够我们定义一些我们日常通过的模板,但对一些完美主义者,(比如我,:-) ),这些还不够,比如我希望生成的日期的格式不带时间,而且月/日都是固定的两位,中间用.分隔,如2008.10.06,由于Visual Studio默认提供的模板参数不能让我们自定义格式,要想实现上面的功能,我们就需要自定义模板参数,这也是我下一篇文章的主题。