Visual Studio Code Snippet Generator

A Code Snippet in Visual Studio allows a developer to type a shortcut and then a block of code is inserted where the cursor is. The block of code contains literal values that can be replaced in the templated snippet.

A commonly used code snippet is prop.

  • In the class the developer types prop and hits the tab key
  • A property declaration replaces the text and the developer has the option to replace the variable type and variable name
  • The code snippet highlights in turn the variable type (int) and then the variable name (MyProperty) by hitting the tab key and typing
public int MyProperty { get; set; }

What Makes a Code Snippet

A code snippet is written using XML and is made up from the following elements

  • Header Element
    • Title - The friendly name of the code Snippet
    • Description - a description of the code Snippet
    • Author - who wrote the snippet
    • Shortcut - the text to type to activate the code snippet
    • HelpUrl - optional help url for the snippet
    • Keywords - allows code snippets to be grouped in Visual Studio
    • SnippetTypes - allows code snippets to be grouped in Visual Studio
  • Snippet
    • Declarations
      • Literal
        • ID - text that is to be replaced in the code section
        • Tooltip - tooltip to show when typing
        • Default - is the default value to put in the code block (for replacing)
    • Code
      • Language - what language the snippet is for C#, VB, JavaScript etc
      • Delimiter - the delimiter for the literals that will be replaced in code e.g $
      • CData Section - this is where you write your code template and add the literals that need replacing

Create a Serialized Property Snippet

A common task when creating serializable classes in C# is to add attributes for XML and JSON serialization that may differ from the property name. for instance

public string CustomDescription {get;set;}

We are going to create a snippet that takes the following literals (values) which will be replaced when the code snippet is accessed

  • Xml Name
  • Json Name
  • Variable Type
  • Variable Name

In addition the shortcut we are going to use is propjx. Below is the output that we want to create.

<?xml version="1.0" encoding="utf-16"?>
<CodeSnippets xmlns="">
  <CodeSnippet Format="1.0.0">
      <Title>Serialiazable Property</Title>
      <Author>Jonathan Purdom</Author>
      <Description>Public Property with XML and JSON Serializable Attributes</Description>
      <CodeSnippetTypes />
      <KeyWords />
      <Code Language="CSharp" Delimiter="$"><![CDATA[

	public $VarType$ $VarName$ {get; set;}

          <Tooltip>XML Element Name of the Property</Tooltip>
          <Tooltip>Json Element Name of the Property</Tooltip>
          <Tooltip>Property type</Tooltip>
          <Tooltip>Variable Name</Tooltip>

Fluent Snippet Builder

I have creates a fluent class that will build up the XML code snippet element. It is designed to run in LINQPad. The fluent class has the following methods for creating a code snippet

  • SetHeaderAuthorAndTitle(author, title)
  • SetHeaderDescriptionAndHelpUrl(description, optional helpUrl)
  • SetHeaderShortcut(shortcut)
  • SetSnippetLanguage(enum) - Optional defaults to CSharp
  • AddLiteralValue(id, tooltip, defaultValue) - Call for each variable (literal) you want to add to the template
  • GetSnippetTemplate - returns the XML for the Code Snippet (you can then add your template into the CData section)

Calling Fluent Snippet Builder

Using LINQPad this is the code to create the Snippet above.

void Main()
	var fsb = new FluentSnippetBuilder()
		.SetHeaderAuthorAndTitle("Jonathan Purdom","Serialiazable Property")
		.SetHeaderDescriptionAndHelpUrl("Public Property with XML and JSON Serializable Attributes")
		.AddLiteralValue("XmlName","XML Element Name of the Property","name")
		.AddLiteralValue("JsonName","Json Element Name of the Property","name")
		.AddLiteralValue("VarType","Property type","string")
		.AddLiteralValue("VarName","Variable Name","MyProperty");
	var output = fsb.GetSnippetTemplate();


Fluent and Serializable Classes

These are the classes used to create the Code Snippet

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;

#region Fluent Class
public class FluentSnippetBuilder
	private CodeSnippet _snippet;
	private string _delimiter = "$";

	public FluentSnippetBuilder()
		=> _snippet = new CodeSnippet();

	public string GetSnippetTemplate()
		var literals = _snippet.Snippet.Declerations.Select(c => $"{_delimiter}{c.Id}{_delimiter}").ToList();
		var template = new XmlNode[] { new XmlDocument().CreateCDataSection(string.Join("\n",literals))};

		_snippet.Snippet.Code.Template = template;

		var codeSnippets = new CodeSnippets();
		codeSnippets.CodeSnippet = _snippet;
		return SerializeSnippet<CodeSnippets>(codeSnippets);

	#region Populate Header Values
	public FluentSnippetBuilder SetHeaderAuthorAndTitle(string author, string title)
		if (string.IsNullOrWhiteSpace(author)) throw new ArgumentNullException(nameof(author));
		if (string.IsNullOrWhiteSpace(title)) throw new ArgumentNullException(nameof(title));
		_snippet.Header.Author = author.Trim();
		_snippet.Header.Title = title.Trim();
		return this;

	public FluentSnippetBuilder SetHeaderDescriptionAndHelpUrl(string description, string helpUrl = "")
		if (string.IsNullOrWhiteSpace(description)) throw new ArgumentNullException(nameof(description));
		_snippet.Header.Description = description.Trim();

		if (!string.IsNullOrWhiteSpace(helpUrl))
			_snippet.Header.HelpUrl = helpUrl.Trim();

		return this;

	public FluentSnippetBuilder SetHeaderKeywords(IEnumerable<string> keywords)
		if (keywords is null) throw new ArgumentNullException(nameof(keywords));

			keywords.Select(c => new Keyword { Value = c.Trim()}).ToList());
		return this;

	public FluentSnippetBuilder SetHeaderShortcut(string shortCut)
		if (string.IsNullOrWhiteSpace(shortCut)) throw new ArgumentNullException(nameof(shortCut));
		_snippet.Header.ShortCut = shortCut.Trim();
		return this;

	public FluentSnippetBuilder SetHeaderSnippetTypes(IEnumerable<string> snippetTypes)
		if (snippetTypes is null) throw new ArgumentNullException(nameof(snippetTypes));

			snippetTypes.Select(c => new SnippetType { Value = c.Trim() }).ToList());
		return this;

	#region Populate Snippet Values

	public FluentSnippetBuilder SetSnippetLanguage(LanguageValue language)
		_snippet.Snippet.Code.Language = language.ToString();
		return this;

	public FluentSnippetBuilder ChangeDelimiterFromDefault(string delimiter)
		if (string.IsNullOrWhiteSpace(delimiter)) throw new ArgumentNullException(delimiter);
		_delimiter = delimiter.Trim();
		_snippet.Snippet.Code.Delimiter = _delimiter;
		return this;

	public FluentSnippetBuilder AddLiteralValue(string id, string tooltip, string defaultValue)
		if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id));
		if (string.IsNullOrWhiteSpace(tooltip)) throw new ArgumentNullException(nameof(tooltip));
		if (string.IsNullOrWhiteSpace(defaultValue)) throw new ArgumentNullException(nameof(defaultValue));

		_snippet.Snippet.Declerations.Add(new Literal {Id = id, ToolTip = tooltip, DefaultValue = defaultValue});

		return this;


	#region Private Methods
	private string SerializeSnippet<T>(T value)
		var serializer = new XmlSerializer(typeof(T));

		using var writer = new StringWriter();
		serializer.Serialize(writer, value);
		return writer.ToString();

#endregion Fluent Class

#region Serializeable Classes
[XmlRoot(Namespace = "")]
public class CodeSnippets
	public CodeSnippet CodeSnippet { get; set; } = new CodeSnippet();

public class CodeSnippet
	public string Format { get; set; } = "1.0.0";

	public CodeSnippetHeader Header {get;set;} = new CodeSnippetHeader();

	public Snippet Snippet{ get; set; } = new Snippet();

public class CodeSnippetHeader
	public string Title { get; set; }

	public string Author { get; set; }

	public string Description { get; set; }

	public string HelpUrl { get; set; }

	public List<SnippetType> CodeSnippetTypes { get; set; } = new List<SnippetType>();

	public List<Keyword> KeyWords {get;set;} = new List<Keyword>();

	public string ShortCut {get;set;}

public class Snippet
	public Code Code {get;set;} = new Code();

	public List<Literal> Declerations {get;set;} = new List<Literal>();

public class Literal
	public string Id { get; set; }

	public string ToolTip { get; set; }

	public string DefaultValue {get;set;}

public class Code
	public string Language { get; set; } = "CSharp";

	public string Delimiter { get; set;} = "$";

	public XmlNode[] Template {get;set;}

	public Code()
		Template = new XmlNode[] { new XmlDocument().CreateCDataSection("")};


public class SnippetType
	public string Value { get; set; }

public class Keyword
	public string Value { get; set; }

#region Enum Values
public enum LanguageValue


Post Running Script

When you run the script with the literal values it will create the CData section with the literal values in only. At that point you then need to replace the code around the variable values. Final step is to save it as an XML documnent.

Output from Fluent Builder

<Code Language="CSharp" Delimiter="$"><![CDATA[$XmlName$

Output with Code Template

So in the code template we have added the attributes that we want to code generate and then placed the literals that we want to replace when the snippet is activated

<Code Language="CSharp" Delimiter="$"><![CDATA[

	public $VarType$ $VarName$ {get; set;}


Installing a Code Snippet

In Visual Studio open the Code Snippets Manager (Tools > Code Snippets Manager). Set the language for the code snippet. Click the import button and find the code snippet xml file and then choose where to add the snippet and click Ok to finish