6

I created a simple code snippet in c# which add the line

Debug.WriteLine("");

now the next step would be, when you use the snippet, to autocreate

using System.Diagnostic;

is there any way to automatically create the reference? I tried and set the snippets Reference and Import elements this way:

<Snippet>
  <References>
    <Reference>
      <Assembly>System.dll</Assembly>
    </Reference>
  </References>
  <Imports>
    <Import>
      <Namespace>System.Diagnostic</Namespace>
    </Import>
  </Imports>
      .
      .
      .
</Snippet>

but it doesn't work

Fabio Marcolini
  • 2,315
  • 2
  • 24
  • 30
  • 2
    Not an answer but if you want to quickly add the `using` statement, you can just press `CTRL + .` on the class name and then press `enter` to select which statement to use. – keyboardP Jul 01 '13 at 15:50
  • @keyboardP well, if i don't find a cleaner way to do that I might use the end cursor inbetween Debug so I'm ready to import – Fabio Marcolini Jul 01 '13 at 15:54

4 Answers4

1

Unfortunately, Import only works for VB projects. It is explained on MSDN.

Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • 1
    Weird, I would have never believed it unless I saw it there myself. – Scott Chamberlain Jul 01 '13 at 16:07
  • 1
    The link provided by Abe Heidebrecht is broken. This is the new source: [MSDN](https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet) –  Jul 19 '17 at 16:39
  • C# is supported now (not sure since when): https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2017#add-references-and-imports – Caio Campos Dec 03 '18 at 00:43
1

A little late to the game, but this does work for C# now :)

<References>
    <Reference>
        <Assembly>System.dll</Assembly>
    </Reference>
</References>
<Imports>
    <Import>
       <Namespace>System.Diagnostic</Namespace>
    </Import>
</Imports>

Place those inside the <Snippet> section. The docs even mention (This works for C# as well.).

https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2017#add-references-and-imports

PS: if you want to add more than one import, do it like this:

<Imports>
    <Import>
        <Namespace>System.Diagnostic</Namespace>
    </Import>
    <Import>
        <Namespace>System.Reflection</Namespace>
    </Import>
</Imports>
Caio Campos
  • 339
  • 5
  • 10
0
System.Diagnostics.Debug.WriteLine("");

Instead of inserting a reference you could use the fully qualified name in your snippet.

SLuck49
  • 353
  • 1
  • 8
0

I had a look at the mbox snippet created by microsoft and came up with the following:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
     <Header>
       <Title>dw</Title>
       <Shortcut>dw</Shortcut>
       <Description>Code snippet for System.Diagnostics.Debug.WriteLine</Description>
       <Author>MisaThinksMeDidIt</Author>
       <SnippetTypes>
         <SnippetType>Expansion</SnippetType>
       </SnippetTypes>         
     </Header>
     <Snippet>    
       <Declarations>
         <Literal>
            <ID>string</ID>
            <ToolTip>String to display</ToolTip>
            <Default>"Test"</Default>
         </Literal>
         <Literal Editable="false">
           <ID>SystemDiagnosticsDebugWriteLine</ID>
           <Function>SimpleTypeName(global::System.Diagnostics.Debug)</Function>
         </Literal>
       </Declarations>
       <Code Language="csharp"><![CDATA[$SystemDiagnosticsDebugWriteLine$.WriteLine($string$);$end$]]></Code>
     </Snippet>
  </CodeSnippet>
</CodeSnippets>
naderunner
  • 41
  • 7