Using CodeSmith dropdown template properties

I created a CodeSmith template the other day to generate a Data Provider method that returns a single value from a query.  It’s not a method I’ll use a lot since if retrieving data I’m usually populating an object, but still a task I wanted to automate.  Below is an example of the template’s generated output.


I wanted the ability to return integers, strings and DateTime types, so the best way to do that was using a dropdown selection in the CodeSmith template.  The dropdown is shown in the template execution dialog.


The dropdown is populated from an Enumerator created in the template’s CodeBehind


public enum DataSelectType

declared as type DataSelectType in the template .CST header.


<%@ Property Name=”DataTypeReturned”
        Category=”2. Return Value Settings”
        Description=”Type of Data returned: int, string, DateTime currently supported” %>


To create the first line in the method, DateTime completeTime = Convert.ToDateTime(“6/9/1969”); the .CST code would be

<%= InitVariable(DataTypeReturned, VarReturned) %>


which calls a method in the .CST’s <script /> area called InitVariable()


public string InitVariable(DataSelectType dataSelectType, string _varReturned)
    switch (dataSelectType)
        case DataSelectType.String:
            return “String ” + _varReturned + ” = String.Empty;”;
        case DataSelectType.DateTime:
            return  “DateTime ” + _varReturned + ” = Convert.ToDateTime(\”6/9/1969\”);”;

        default: return “int ” + _varReturned + “= 1;”;

You’ll find examples of dropdown property selections in the CodeSmith sample templates.

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.