There's a way to create a code snippet with automatically create a using reference?
Asked Answered
D

4

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

Defant answered 1/7, 2013 at 15:46 Comment(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.Queenqueena
@Queenqueena 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 importDefant
S
1

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

Sloat answered 1/7, 2013 at 16:3 Comment(3)
Weird, I would have never believed it unless I saw it there myself.Vogt
The link provided by Abe Heidebrecht is broken. This is the new source: MSDNMetrify
C# is supported now (not sure since when): learn.microsoft.com/en-us/visualstudio/ide/…Nomology
N
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>
Nomology answered 3/12, 2018 at 0:4 Comment(0)
A
0
System.Diagnostics.Debug.WriteLine("");

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

Aksum answered 1/7, 2013 at 17:6 Comment(1)
yeah i know but I want to avoid that i find that really uglyDefant
G
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>
Granadilla answered 19/7, 2013 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.