Property directive
To declare a property in a CodeSmith Generator template, use a Property directive. This directive declares a property named ClassName of type System.String:
<%@ Property Name="ClassName" Type="System.String" Category="Context" Description="The name of the class to be generated." %>
Attributes
Name and Type are required; all other attributes are optional.
| Attribute | Description | Required |
|---|---|---|
Name |
The property name shown in the property grid. It's also the name of the variable that holds the value in template code, so it must be a legal identifier in the template's language. For example, in a C# template it must follow the rules for C# variable names. | Yes |
Type |
The .NET type of the property. Any .NET type works, but complex types may need an Editor so that the user can enter a value. |
Yes |
Default |
The default value. If you omit it, the property has no default value. | No |
Category |
The property grid category the property appears in. | No, defaults to Misc |
Description |
Text shown at the bottom of the property grid when the property is selected. | No |
Optional |
Whether the user can leave the property empty. When False, Generator doesn't generate until the user supplies a value; when True, a value isn't required. |
No |
Editor |
The editor the property grid uses for this property. This is equivalent to putting an [Editor] attribute on a property in code. See Choosing an editor. |
No |
EditorBase |
The base type of the editor. | No, defaults to UITypeEditor |
Serializer |
The IPropertySerializer type used to save the property's value. This is equivalent to putting a [PropertySerializer] attribute on a property in code. See Adding property set support. |
No |
OnChanged |
The event handler to call when the property's value changes. | No |
DeepLoad |
For SchemaExplorer types only. When True, SchemaExplorer loads all the schema information up front instead of making many round trips to the database. |
No |
IncludeFunctions |
For SchemaExplorer types only. When True, the pickers also list SQL functions. See Using functions. |
No |
Note
For scalar types, use Base Class Library type names such as String or Int32, not language keywords such as string or int.
Choosing an editor
Specify the editor's type name and assembly in the Editor attribute. For example, this property uses the .NET folder picker:
<%@ Property Name="ModelDirectory" Type="System.String" Editor="System.Windows.Forms.Design.FolderNameEditor, System.Design" %>
For a file picker, use Editor="System.Windows.Forms.Design.FileNameEditor, System.Design". Generator uses only the type name from the Editor value; any version information is ignored.
For the editors that ship with Generator, see Adding designer support.
Declaring a property in code
You can also declare a property in a <script runat="template"> block or a code-behind file, just like a property in any class. Attributes describe how the property appears in the property grid:
private string aliasFilePath;
[Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Category("01. General")]
[Optional]
[DefaultValue("")]
[Description("Optional File Path to a table/object alias file.")]
public string AliasFilePath
{
get { return this.aliasFilePath; }
set { this.aliasFilePath = value; }
}
| Attribute | Purpose |
|---|---|
Editor |
The editor to use in the property grid. |
Category |
The property grid category the property belongs to. |
Optional |
Marks the property as optional. |
DefaultValue |
The default value of the property. |
Description |
The description shown when the property is selected. |