BlackWaspTM
Visual Studio
VS 2005+

Using the Simplest Type Name in a Snippet

Code snippets are used to insert commonly-used fragments into source code files. When those snippets include type names the inserted name must be fully qualified or relative to a using directive. This can be automated with the SimpleTypeName function.

SimpleTypeName Function

Code snippets provide a useful mechanism for inserting boilerplate code into source files, potentially improving productivity. These code snippets can include replacements, which guide you through overtyping elements of the inserted snippet, and functions, which insert text that varies according to various parameters. I've described one such function previously in the article, "Inserting the Class Name in a Code Snippet".

When you create a code snippet that inserts the name of a class or structure, the exact name that's used should depend upon the context. If you have a using directive that references the namespace containing the inserted class, it is better to use the class name alone. If there is no such using directive, you would wish for the fully qualified name to be added to the code. You can have the code snippet make this determination automatically using the SimpleTypeName function.

To use SimpleTypeName, you create a literal with the Function element containing the function. SimpleTypeName requires a parameter to be specified. This is the fully qualified name of the type that you wish to use within the code of the snippet. You can then insert a placeholder with the literal's name into the snippet code. When the code snippet is used, Visual Studio checks for a suitable using directive and inserts either the simple type name or the fully qualified name automatically.

We can see this using the following snippet. This inserts a parallel for loop into a C# code file with replacements for the start and end of the loop and the loop control variable. The fourth literal uses the SimpleTypeName function to reference the Parallel class using its fully qualified name, "System.Threading.Tasks.Parallel". The result of the function replaces all $Parallel$ placeholders in the snippet when inserted into a source code file.

NB: This snippet is suitable for Visual Studio 2010 or later at it uses .NET 4.0 code. However, the use of the SimpleTypeName function only requires Visual Studio 2005 or later.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0"
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Parallel For Loop</Title>
    <Author>BlackWasp</Author>
    <Shortcut>pfor</Shortcut>
    <Description>Inserts a parallel For loop</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Start</ID>
        <Default>0</Default>
      </Literal>
      <Literal>
        <ID>End</ID>
        <Default>100</Default>
      </Literal>
      <Literal>
        <ID>Iterator</ID>
        <Default>i</Default>
      </Literal>
      <Literal Editable="false">
        <ID>Parallel</ID>
        <Function>SimpleTypeName(System.Threading.Tasks.Parallel)</Function>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[$Parallel$.For($Start$, $End$, $Iterator$ =>
    {
        // Insert Parallel Code
        $selected$$end$
    });]]>
    </Code>
  </Snippet>
</CodeSnippet>
11 November 2011