VS2005中通过code snippet定制类模板

[自从去年4月到新公司后就没有再写BLOG了,不过还好没被DUDU除名;-)。www.andrewbao.com]
曾经在VS2003中为了定制项模板费了很多事才实现,如果你对VS2003中怎么实现项模板可以参考我过去的一篇Blog 创建自定义的Visual Studio项模板
随着VS2005的发布,我们有了更多的选择,对于定制某种类的样式来说,可以简单的通过创建Code Snippet样式来实现。下面还是以应用Singleton模式的类为例。
首先我们创建一个文件,比如名字叫Singleton.snippet,内容如下:

    1 <?xml version="1.0" encoding="utf-8" ?>

    2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

    3 <CodeSnippet Format="1.0.0">

    4           <Header>

    5                    <Title>Singleton</Title>

    6                    <Shortcut>Singleton</Shortcut>

    7                    <Description>singleton class</Description>

    8                    <Author>AndrewBao</Author>

    9                    <SnippetTypes>

   10                             <SnippetType>Expansion</SnippetType>

   11                    </SnippetTypes>

   12           </Header>

   13           <Snippet>

   14                    <Declarations>

   15                             <Literal>

   16                                       <ID>classname</ID>

   17                                       <ToolTip>Class name</ToolTip>

   18                                       <Default>SingletonSample</Default>

   19                             </Literal>

   20                             <Literal>

   21                                       <ID>type</ID>

   22                                       <ToolTip>Instance Type</ToolTip>

   23                                       <Default>SingletonSample</Default>

   24                             </Literal>

   25                             <Literal>

   26                                       <ID>field</ID>

   27                                       <ToolTip>Single Instance of SingleSample</ToolTip>

   28                                       <Default>MySingleObj</Default>

   29                             </Literal>

   30                             <Literal>

   31                                       <ID>method</ID>

   32                                       <ToolTip>Get Instance Method</ToolTip>

   33                                       <Default>GetInstance</Default>

   34                             </Literal>

   35                    </Declarations>

   36                    <Code Language="csharp">

   37                             <![CDATA[class $classname$

   38           {

   39             static private $classname$ $field$ = null;           

   40             protected $classname$()

   41             {

   42             }

   43             public static $classname$ $method$()

   44             {

   45                 if ($field$ == null)

   46                     $field$ = new $classname$();

   47                 return $field$;

   48             }

   49           }]]>

   50                    </Code>

   51           </Snippet>

   52 </CodeSnippet>

   53 </CodeSnippets>

然后通过VS2005中Tools->Code Snippets Manager将此snippet模板导入,如下图

OK,开始应用这个模板,应用的方式和结果如下图:

怎么样,很简单吧! 还有两个相关资源可以使用:

1.gotcodesnippets 分享CodeSnippet模板的网站
2.Snippy - VS2005 Code Snippet Editor

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值