Udostępnij za pośrednictwem


Code Element (IntelliSense Code Snippets)

Provides a container the short code blocks of IntelliSense Code Snippets.

<CodeSnippets>
  <CodeSnippet>
    <Snippet>
      <Code>

<Code Language="Language"
    Kind="method body/method decl/type decl/page/file/any"
    Delimiter="Delimiter">
    Code to insert
</Code>

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute

Description

Delimiter

Optional attribute.

Specifies the delimiter used to describe literals and objects in the code. By default, the delimiter is $.

Kind

Optional attribute.

Specifies the kind of code that the snippet contains and, therefore, the location at which a code snippet must be inserted for the code snippet to compile. The values available are method body, method decl, type decl, file, and any. For more information, see the section "Kind Attribute" in the following table.

Language

Required attribute.

Specifies the language of the code snippet. The values available are VB, CSharp or XML. For more information, see the section "Language Attribute" in the following table.

Note   The Intellisense Code Snippet schema does not support C++.

Kind Attribute

Value

Description

method body

Specifies that the code snippet is a method body, and therefore, must be inserted inside a method declaration.

method decl

Specifies that the code snippet is a method, and therefore, must be inserted inside a class or module.

type decl

Specifies that the code snippet is a type, and therefore, must be inserted inside a class, module, or namespace.

file

Specifies that the snippet is a full code file. These code snippets can be inserted alone into a code file, or inside a namespace.

any

Specifies that the snippet can be inserted anywhere. This tag is used for code snippets that are context-independent, such as comments.

Language Attribute

Value

Description

VB

Identifies a Visual Basic code snippet.

CSharp

Identifies a Visual C# code snippet.

XML

Identifies an XML code snippet.

Child Elements

None.

Parent Elements

Element

Description

Snippet

Contains the references, imports, declarations, and code for the code snippet.

Text Value

A text value is required.

This text specifies the code, along with the literals and objects, that you can use when this code snippet is inserted into a project.

Remarks

Two reserved words are available for use in the text of the Code element: $end$ and $selected$. $end$ marks the location to place the cursor after the code snippet is inserted. $selected$ represents text selected in the document that is to be inserted into the snippet when it is invoked. For example, if you had:

$selected$ is a great color. I love $selected$.

and the word "Blue" was selected when you invoked the template, you would get:

Blue is a great color. I love Blue.

All other $ keywords are dynamically defined in the <Literal> and <Object> tags.

Example

The Code element of the following code snippet shows how to write a Visual C# code snippet that creates a SqlDataAdapter. The literal and object defined in the Declarations element are referenced in the Code element by the value of their ID element. In this example, the literal is referenced as $SQL$ and the object is referenced as $Connection$.

<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Create a data adapter</Title>
            <Description>Creates a SqlDataAdapter object.</Description>
            <Author>Microsoft Corporation</Author>
            <Shortcut>createadapter</Shortcut>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>SQL</ID>
                    <ToolTip>Replace with a SQL connection string.</ToolTip>
                    <Default>"SQL connection string"</Default>
                </Literal>
                <Object>
                    <ID>Connection</ID>
                    <Type>System.Data.SqlClient.SqlConnection</Type>
                    <ToolTip>Replace with a connection object in your application.</ToolTip>
                    <Default>dcConnection</Default>
                </Object>
            </Declarations>
            <Code Language="CSharp">
                <![CDATA[
                    daCustomers = new SqlClient.SqlDataAdapter();
                    selectCommand = new SqlClient.SqlCommand($SQL$);
                    daCustomers.SelectCommand = selectCommand;
                    daCustomers.SelectCommand.Connection = $Connection$;
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

See Also

Concepts

Code Snippets Schema Reference