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.
-
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.
-
Look through the template source. Generator's template language is very similar to ASP.NET:
- The file starts with a
CodeTemplatedirective and a set ofPropertydirectives that declare the properties you saw in the property sheet. - A
<script runat="template">block holds helper code, such as theGetFileName()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 theClassNameproperty, a colon, and the value of theBaseClassproperty. Code blocks such as<% foreach(NameValueEntry entry in this.Arguments) { %>repeat parts of the output once for each argument. - The file starts with a
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