Set properties

A code generator that produced exactly the same code every time wouldn't be very useful. CodeSmith Generator templates use properties to let you customize the generated code. When you open a template from Template Explorer, its property sheet lists the properties the template uses, grouped into categories. You supply values for them before Generator generates the code.

The CustomException template uses several kinds of properties:

  • String properties, such as ClassName, accept any text you type.
  • Enumerated properties, such as Accessibility, show a drop-down list of choices when you click them.
  • Collection properties, such as Arguments, open an editor where you add Name=Value entries.
  • Number properties, such as ErrorCode, accept a number.

When you select a property, its description appears at the bottom of the property sheet.

For this tutorial, fill in the property sheet like this:

Property Value What it does
ClassName OrderNotFoundException The name of the generated class, and of the output file.
ClassNamespace Contoso.Orders The namespace for the class. Leave it empty to generate the class without a namespace.
BaseClass Exception The exception class to derive from (the default).
Accessibility Public The accessibility of the class.
Arguments OrderId=int Extra details the exception carries, as Name=Type entries. Each one becomes a constructor parameter and a read-only property.
ArgumentMessage Order {0} was not found. The message format used by ToString() for the arguments.

Leave ErrorCode at its default value.

Tip

To find a property quickly, type part of its name in the Filter box above the property sheet.

Properties can be based on many kinds of metadata, not just text. For example, a property can list all the tables in a database so you can pick one when you generate. See Driving templates with metadata.

Next: Generate code