In this article, we will look into steps to be followed for creating and registering a custom code snippet for C# in Visual Studio 2015. Code snippets are helpful for distributing and re-using code. It saves time in writing repetitive code. All we need is to create an XML file with all details for creating a custom code snippet.
Let's create the following XML code and name it
AddClass-Snippet.snippet:
In the header section, we will set attributes like Title, Author etc.
In Reference section, we can add reference to assemblies and namespaces to be included. Here, I added a reference to
System.ComponentModel.DataAnnotations.dll and imported same. So, when I use this snippet, it will include that assembly in my project and add using statement as well.
In Declarations section, we can declare literals or objects that need to be replaced within snippet and the same can be referred in the snippet using $name$. Here, I declared class name literal and object literal.
Under Code section, we can mention Language and add code within CData as shown below:
Basically, this snippet will insert a class and allows us to give a name for it and the same will be applied to the static variable as well.
Here's the complete code:
- <?xml version="1.0" encoding="utf-8"?>
- <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
- <CodeSnippet Format="1.0.0">
- <Header>
- <Title>Add Class</Title>
- <Author>Sateesh</Author>
- <Description>Adds boilerplate code for creating a class</Description>
- <Shortcut>addclass</Shortcut>
- </Header>
- <Snippet>
- <References>
- <Reference>
- <Assembly>System.ComponentModel.DataAnnotations.dll</Assembly>
- </Reference>
- </References>
- <Imports>
- <Import>
- <Namespace>System.ComponentModel.DataAnnotations</Namespace>
- </Import>
- </Imports>
- <Declarations>
- <Literal>
- <ID>classname</ID>
- <ToolTip>Replace with specified name</ToolTip>
- <Default>MyClass</Default>
- </Literal>
- <Object>
- <ID>Object</ID>
- <Type>System.Object</Type>
- <ToolTip>Replace with a object in your application.</ToolTip>
- <Default>myObject</Default>
- </Object>
- </Declarations>
- <Code Language="CSharp">
- <![CDATA[
- public class $classname${
- private static $classname$ myObj= new $classname$();
- Object con = $Object$;
- public int Id
- {
- get; set;
- }
- [StringLength(20)]
- public string Name
- {
- get; set;
- }
- [EmailAddress]
- public string Email
- {
- get; set;
- }
- }
- ]]>
- </Code>
- </Snippet>
- </CodeSnippet>
- </CodeSnippets>
Let’s import into VS 2015 and use it, Open VS 2015 and click on Tools, Code Snippet Manager and click on Import and point to snippet file:
Select locations, under which snippet is to be saved. Click
Finish and create a new console application and use our new code snippet by typing add class and pressing TAB:
This will add reference and import
System.ComponentModel.DataAnnotations.dll into the project.
I am ending things here, I hope this article will be helpful for all.
Read more articles on Visual Studio: