Code snippet Technology in

Source: Internet
Author: User

Note: This article is the translation of the msdn article, the original http://msdn.microsoft.com/zh-cn/library/ms379562 (vs.80 ). aspx was originally intended to be consistent with the original text, but the time limit was omitted. The translation was not very appropriate. please correct me if not.
    
Summary: This article explores the code snippet Technology in vs2005 and vs2005 Express versions (including vs2008, but there is not much difference. I will use brackets to prompt the difference, this includes a complete process from creation to registration of custom code extensions.

I. Understand the role of code snippet

Both vs2005 and Visual C # express support a technology called code snippet, which is the basis of the following two code generation technologies:

1. Expansion template (translated as a code segment in Chinese)

2. Around

In short, the only reason for the existence of code snippet technology is the productivity of developers. Extended templates and code blocks can be generated by developers in any of the following ways.

· Ide editing-> smart sensing menu

· Shortcut keys

· Right-click

· Enter a registered snappet shortcut

Ii. Simple Example

1) code segment example

To illustrate the function of the expansion template, assume that you have already seen a class C # (called sportscar) and want to quickly add an attribute to it. In addition to manual input, you can use the method mentioned above to activate the property extension (expansion), for example, IDE editing-> smart sensing-> insert a code segment. In the selection list, Select Property (prop in vs2008) for example:

You can double-click or press the tab key twice. Once activated, the following code is generated (vs2005 generates a private field)


Public int myproperty {Get; set ;}

 

For example:

The yellow part (different colors in vs2008) can be used to edit the attribute name (you can use the tab key to jump between highlighted fields)

2) Examples

To illustrate, suppose you want to place a code segment in the # region/# endregion command, select the code segment first, right-click and select insert code segment.

In the displayed list, select # region and edit region and name.

Iii. Location of the code snippet File

You can find a large number of built-in code snippet files in the following position. vs2005 ends with. XML, and vs2008 ends with. snippet.

<Drive>: \ Program Files \ Microsoft Visual Studio 8 \ Vc # \ expansions \ 1033 \ expansions

<Drive >:\ Program Files \ Microsoft Visual Studio 9.0 \ Vc # \ snippets \ 1033 \ Visual C #
// <Drive> drive name

Iv. Structure of the code snippet File

Code

<? XML version = "1.0" encoding = "UTF-8"?>
<Codesnippet format = "1.0.0">
<Header>
<Title> class </title>
<Shortcut> class </shortcut>
<Description> expansion snippet for class </description>
<Snippettypes>
<Snippettype> expansion </snippettype>
<Snippettype> surroundswith </snippettype>
</Snippettypes>
</Header>
<Snippet>
<Declarations>
<Literal default = "true">
<ID> name </ID>
<Tooltip> class name </tooltip>
<Default> myclass </default>
</Literal>
</Declarations>
<Code language = "CSHARP" format = "CDATA">
<! [CDATA [class $ name $
{
$ Selected $ end $
}]>
</Code>
</Snippet>
</Codesnippet>

 

Contains a root node named codesnippet and two byte points: Header, snippet, (codesnippets node exists outside codesnippet in vs2008)

<Header> node and sub-byte

The header node is used to describe the basic attributes of code snappet, including the following byte points

            

<Header> sub-element Definition
<Title> The display title for the code snippet.
<Shortcut> Defines the shortcut for the code snippet.

In the IDE, type in the shortcut name to select the snippet followed byTab. If you type a partial partition cut name ('cla' rather than 'class' for example), you will need to tab twice; once to complete the named expansion and again to insert the snippet.

<Description> A human-readable description of the snippet, which is displayed when selecting a snippet from the IDE.
<Snippettype> Specifies which category the code snippet belongs to (expansion, surroundswith or refactoring). do note that a single code snippet may belong to more than one group.

Microsoft ides use this value to determine which context menu will be used to display the code snippet.

<Snippet> nodes and subnodes

The snippet node is mainly used to define two parts:

A. the variable is the part with a high volume during insertion.

B. Code skeleton

The subnode contains two following literal, code:

<Declarations>
<Literal default = "true">
<ID> name </ID>
<Tooltip> class name </tooltip>
<Default> myclass </default>
</Literal>
</Declarations> <code language = "CSHARP" format = "CDATA">
<! [CDATA [class $ name $
{
$ Selected $ end $
}]>
</Code>

 

<Literal> subnode Definition
<ID> Variable Representation
<Tooltip> Move the cursor to the above prompt.
<Default> The default value of the variable.

<Code> node

Code is used to define the code to be inserted. In the <snippet> node above, we can see that some of the Code is surrounded by $, this syntax is used to reference the variables defined in the <ID> node of <literal> above (note that the names are consistent)

About $ selected $ end $

$ Selected $ is used by Vs to replace the selected content when a code segment (around) is inserted. $ end $ is the position where the mouse stays after the insertion is complete.

5. Register a custom code snappet

Two methods:

Copy the file to <drive >:\ Program Files \ Microsoft Visual Studio 8 (9) \ Vc # \ expansions \ 1033 \ expansions.
(Vs2008 is: X: \ Program Files \ Microsoft Visual Studio 9.0 \ Vc # \ snippets \ 1033 \ Visual C #).Tools | code snippet ManagerIn the displayed menu, Add.

Attachment: Below is the snappet of the action method of Asp.net MVC written by myself. After registration, enter the action in vs to activate it.

Because of the restrictions on uploading files to a blog, I changed action. XML to XML. After downloading the file, I changed it to. snappet.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.