您的位置:知识库 » .NET技术

提高效率 用好Visual Studio 2010自定义代码段

来源: 51CTO  发布时间: 2010-12-06 10:20  阅读: 1517 次  推荐: 0   原文链接   [收藏]  
摘要:Visual Studio允许开发人员将一段代码保存起来以便以后随时调用(重复使用),即所谓的自定义代码段,这样不仅可以节约时间,还可以实现代码共享。

  我在演讲时发现这个功能也相当有用,不用现场敲代码,直接调出非常方便。Visual Studio 2010增强了自定义代码段功能,使创建自定义代码段的操作更加简单了。

  有两种类型的代码段:

  ◆在游标中插入的Expansion自定义代码段

  ◆围绕选定代码的SurroundsWith自定义代码段

  创建自定义代码段

  首先在项目中插入一个新的XML文件,取名为TryCatchFinally.snippet,注意文件名的后缀是.snippet,然后在编辑器窗口点击右键,选择“插入代码段”*“代码段”,创建一个基本的XML代码段模板,代码如下:

<CodeSnippet Format="1.0.0"
xmlns
="http://schemas.microsoft.com/VisualStudio2005/CodeSnippet">
<Header>
<Title>title</Title>
<Author>author</Author>
<Shortcut>shortcut</Shortcut>
<Description>description</Description>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<Default>value</Default>
</Literal>
</Declarations>
<Code Language="XML">
<![CDATA[<test>
<name>$name$</name>
$selected$ $end$
</test>]]>
</Code>
</Snippet>
</CodeSnippet>

  正如你所看到的,默认的模板包括两个代码段类型,因为我这里是要创建一个Expansion代码段,因此去掉<SnippetType>SurroundsWith</SnippetType>标签。

  然后将title改为“Try Catch Finally”,Shortcut改为“trycf”,Description改为“Adds a try-catch-finally block”。

  Literal标签定义了代码段中可编辑的值,在try-catch-finally代码段中,我们希望用户可修改捕获到的异常(Exception)类型,ID标签是可编辑值的名字,因此将其改为“ExceptionName”,Default标签指定了可编辑字段的默认值,我想捕获所有的异常,因此我将它的值改为“Exception”。

  最后,code小节部分包括代码段插入时自动填充的代码,将language改为“CSharp”,code部分的全部代码如下:

1. <Code Language="CSharp">
2. <![CDATA[
3. try
4. {
5.
6. }
7. catch($ExceptionName$)
8. {
9.
10. }
11. finally
12. {
13.
14. }
15. ]]>
16. </Code>

TryCatchFinally.snippet文件的最终代码如下:

1. <?xml version="1.0" encoding="utf-8" ?>
2. <CodeSnippet Format="1.0.0"
3. xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
4. <Header>
5. <Title>Try Catch Finally</Title>
6. <Author>umair</Author>
7. <Shortcut>trycf</Shortcut>
8. <Description>Adds a try-catch-finally block</Description>
9. <SnippetTypes>
10. <SnippetType>Expansion</SnippetType>
11. </SnippetTypes>
12. </Header>
13. <Snippet>
14. <Declarations>
15. <Literal>
16. <ID>ExceptionName</ID>
17. <Default>Exception</Default>
18. </Literal>
19. </Declarations>
20. <Code Language="CSharp">
21. <![CDATA[
22. try
23. {
24.
25. }
26. catch($ExceptionName$)
27. {
28.
29. }
30. finally
31. {
32.
33. }
34. ]]>
35. </Code>
36. </Snippet>
37. </CodeSnippet>

  在Visual Studio中载入自定义代码段

  在Visual Studio中有两种方法载入上面的自定义代码段:

  最直接的方法是将.snippet文件放在Visual Studio的代码段目录下,默认位置是C:\Users\<UserName>\Documents\Visual Studio 2010\Code Snippets\,这个目录会根据所使用的语言生成对应的子目录,如我们这里使用的C#,因此应该将自定义代码段文件放在Visual C#子目录下,Visual Studio会自动发现新放进去的.snippet文件,无需重启Visual Studio。

  第二种方法是将.snippet文件导入到Visual Studio中,选择“工具”*“代码段管理器”(Ctrl+K,Ctrl+B),在代码段管理器中,点击“导入”按钮,浏览到.snippet文件所在位置,选择它,然后点击“确定”。

  使用自定义代码段

  在Visual Studio的编辑器中,输入自定义代码段名字的前几个字符,按Tab键,在弹出的代码提示窗口中便可看到前面自定义的代码段名称(快捷名称和完全名称Tip提示),如下图所示:

输入try

图 1 输入try,在弹出的代码提示窗口中可看到自己定义的代码段名

  你也可以在编辑器中按CTRL+K,再按CTRL+X调出“插入代码段”菜单,如下图所示:

插入代码段菜单

图 2 插入代码段菜单

  选择菜单中的“My Code Snippets”,再选择前面添加的TryCatchFinally自定义代码段,如下图所示:

插入TryCatchFinally自定义代码段

图 3 插入TryCatchFinally自定义代码段

  正如你所看到的,在Visual Studio 2010中,添加自定义代码段,装载自定义代码段文件和使用自定义代码都变得更加简单了,这一提高生产力的功能你应该多多使用。

0
0

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻