Is there a way to create a code snippet with automatically creating a usage link?

I created a simple code snippet in C # that adds a line

Debug.WriteLine(""); 

now the next step is when you use the fragment to autocreate

 using System.Diagnostic; 

Is there a way to automatically create a link? I tried and set the Reference and Import links this way:

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

but it does not work

+6
source share
4 answers

Unfortunately, Import only works for VB projects. This is due to MSDN .

+1
source
 System.Diagnostics.Debug.WriteLine(""); 

Instead of embedding a link, you can use the full name in your snippet.

0
source

I looked 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> 
0
source

A bit late to the game, but now it works in C # :)

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

Put them in the <Snippet> section. The docs even mention it (this works for C # as well).

https://docs.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> 
0
source

Source: https://habr.com/ru/post/1489125/


All Articles