May 25, 2021 CodeSmith
CodeSmith's core is the template, and what energies the template is the properties, which enable the code template to generate the required code based on the configuration by defining the properties. W hen you use a code template, you must first define values for the properties defined by the template before codeSmith can generate code from the template. S ome properties have default values that can be configured without need. The properties in the template are defined by the Property directive:
<%@ Property Name="ClassName" Type="String" Default="Class1" Category="Context" Description="The name of the class to generate" Optional="true" %>
Introduction to property parameters:
When you configure properties, each property uses a different configuration interface depending on its type and Editor, corresponding to simple types such as Int, String can be edited directly, and for database types you can use Schema Explorer, CodeSmith has defined some property editors in advance, as well as a new property editor that can be customized to define certain special property types, which is described later. Customization is usually not required.
In some cases, if the defined property value is a value in a list, such as in CodeSmith's own template SortList.cst, a property is defined to set visibility for the resulting class:
This can be done by defining an enumeration type:
<script runat="template">
Public Enum AccessibilityEnum
[Public]
[Protected]
[Friend]
[ProtectedFriend]
[Private]
End Enum
</script>
The type of property defined is then defined as this enumerumered type:
<%@ Property Name="Accessibility" Type="AccessibilityEnum"
Category="Options" Description="The accessibility of the class to be generated." %>
Because properties can be defined as optional, you need to detect in the template whether a property has been configured, such as the following defined property as Optional
<%@ Property Name="ClassNamespace" Type="System.String" Optional="True"
Category="Context"
Description="The namespace that the generated class will be a member of." %>
In the template, the script needs to detect whether this property has a value that can be implemented by the following code:
<% if (ClassNamespace != null && ClassNamespace.Length > 0)
{ %>namespace <%= ClassNamespace %>{<% }
%>
As you can see from the above, you can use properties directly in your script without prefixing them (such as $).