CodeSmith 4.0 ActiveSnippet Example

ActiveSnippets are a new feature in CodeSmith 4.0 and much like Visual Studio 2005 snippets.  The CodeSmith 4.0 Press Release describes ActiveSnippets with, “Imagine Visual Studio 2005 snippets, but with the full power of CodeSmith available to execute any logic or access any complex metadata (including database schema and xml data) to control the output of your snippets.”

That’s a very helpful description, I think, but ActiveSnippets are not 100% integrated into the Visual Studio IDE like VS2005 snippets are, where you can type a few characters and Intellisense takes care of the rest.  That said, ActiveSnippets are actually pretty tightly integrated into the VS2005 IDE, so seeing them in action here should be enough to get you started.

First, ActiveSnippet templates are like any other CodeSmith template as far as content.  There may be additional argument handling required in an ActiveSnippet template than a normal template, but other than that they’re pretty much identical. 

ActiveSnippets are accessed in VS2005 through the Tools->CodeSmith menu as shown here.

 

 

CodeSmith ActiveSnippets are created with the ActiveSnippet Configuration tool available from the CodeSmith tools menu and looks like the window below.

 

 

Any time you want to see your ActiveSnippet list, you can use the “Output ActiveSnippet Usage” option which will display the window below.

 

 

We’re going to demo the “CV” ActiveSnippet and enter “cv MySweetEvent ArgOne=string” in our VS2005 source editor.  Now, to get additional info about the ActiveSnippet we can then use “Output ActiveSnippet Usage” again (Ctrl-E, Ctrl-R) and see its usage with parameters.

 

 

Now we’re ready to rock.  With our cursor anywhere on the “cv MySweetEvent One=string” line, we use the Expand ActiveSnippet menu (Ctrl-E, Ctrl-E) and shazam!  The following code appears.

 

#region ‘MySweetEvent’ event definition code
/// <summary>
/// EventArgs derived type which holds the custom event fields
/// </summary>
[Serializable]
public class MySweetEventEventArgs : System.EventArgs
{
/// <summary>
/// Use this constructor to initialize the event arguments
/// object with the custom event fields
/// </summary>
public MySweetEventEventArgs(string one)
{
_one = one;
}

private string _one;

public string One
{
get { return _one; }
set { _one = value; }
}

}

/// <summary>
/// This represents the delegate method prototype that
/// event receivers must implement
/// </summary>
public delegate void MySweetEventEventHandler(object sender, MySweetEventEventArgs e);

/// <summary>
/// TODO: Describe the purpose of MySweetEvent here
/// </summary>
public event MySweetEventEventHandler MySweetEvent;

/// <summary>
/// This is the method that is responsible for notifying
/// receivers that the event occurred
/// </summary>
protected virtual void OnMySweetEvent(MySweetEventEventArgs e)
{
if (MySweetEvent != null)
{
MySweetEvent(this, e);
}
}

#endregion //(‘MySweetEvent’ event definition code)
}
}

Article written by

A long time developer, I was an early adopter of Linux in the mid-90's for a few years until I entered corporate environments and worked with Microsoft technologies like ASP, then .NET. In 2008 I released Sueetie, an Online Community Platform built in .NET. In late 2012 I returned to my Linux roots and locked in on Java development. Much of my work is available on GitHub.