Inspect the template

Now look behind the scenes at the CustomException.cst template itself. The template contains the instructions CodeSmith Generator uses to generate the code.

  1. In Template Explorer, right-click the CustomException template and select Edit.

    The template opens in the default template editor. Generator includes a full-featured Template Editor for writing and testing templates.

  2. Look through the template source. Generator's template language is very similar to ASP.NET:

    • The file starts with a CodeTemplate directive and a set of Property directives that declare the properties you saw in the property sheet.
    • A <script runat="template"> block holds helper code, such as the GetFileName() override that names the output file after the class.
    • The rest of the template is the C# code to generate, with properties and expressions in special tokens.

    For example, this line in the template:

    public class <%= this.ClassName %> : <%= this.BaseClass %>
    

    tells Generator to output the literal text public class, then the value of the ClassName property, a colon, and the value of the BaseClass property. Code blocks such as <% foreach(NameValueEntry entry in this.Arguments) { %> repeat parts of the output once for each argument.

If there's something you don't like about the template, you can change it. For example, you might add XML documentation comments to the generated constructors. Type them into the template and save it, and Generator uses your changed template from then on.

Next: Where to go from here