1. First recognized codesmith
To know how powerful codesmith is, I 'd like to tell you the best way to try it. Although codesmith has many advanced and even complex functions, however, you don't have to master all these functions to use them to generate Code .
In this chapter, you will learn how to use codesmith to generate a piece of useful code-a strongly typed hash table class. Follow me to ensure that you can complete it in less than five minutes, in addition, you can see the powerful functions of codesmith explorer and codesmith studio to generate template code.
2. Start codesmith Explorer
The first step is to start codesmith explorer, just like the function of Windows Explorer to organize folders stored on your computer. The main function of codesmith explorer is to organize template files, start your codesmith Explorer through Select Start> Programs> codesmith 3.0> codesmith explorer. Some sample templates are included in the initial interface of codesmith explorer.
3. Open a template
The template shows the code generation style. codesmith provides some useful basic templates. You can also use the codesmith support site
Download to more templates. You can also use codesmith to develop your own templates.
First, click the plus sign of the root directory folder
Files with the CST extension are the codesmith templates. You may have guessed their general purpose from the names of these templates. For example, the cshashtable. CST template is used to generate the C # hash table class. Double-click the template (or right-click and select execute) to open it.
4. Set attributes
If a code generator generates the same code every time, it will be useless. Of course, if you just treat it as a code memory, it will be another matter. Codesmith can customize attributes for you to generate custom code. When you open a template file using codesmith explorer, the template properties page displays the attributes required by the template. Before codesmith generates code, you must enter the values of these parameters. The cshashtable template requires four string attributes (classname, classnamespace, itemtype, and keytype) and an enumeration type attribute (Accessibility ).. You can enter any value you want for a string type attribute, and you can only select the corresponding value for the enumerated type attribute through the drop-down menu. As the first attempt, we enter the following values:
the biggest advantage of codesmith is that it can define different types of metadata attributes. For example, you can create a database form attribute so that users can select a table in the database and generate code. The features of codesmith in creating database table attributes will be described in later sections.