SCR: http://www.cnblogs.com/elevenWolf/archive/2004/12/01/71391.html
You have used it, Codesmith I think everyone is familiar. Recently used Codesmith Generate your own entity class. You can also use it to generate Html Pages and documents.
The following describes how to generate an object class:
I. first introduce Code Smith 2.6 , Code Smith 2.6 After installation 3 Items EXE:
1. Codesmith.exe That is Codesmith Explorer , Visualized generationCodeTool, free of charge, no time limit
2. Codesmithconsole.exe In Command Run in mode, free of charge, no time limit
3. Codesmithstudio.exe Tool used to edit the template to check whether the template syntax is correct. Trial 30 Day.
Codesmith Template support C # , VB. NET Syntax, which can be called . Net Class Library, syntax and . Net It is basically the same.
Ii. Create a template
1. I have selected C # As the template development language.
<% @ Codetemplate Language = "C #" targetlanguage = "text" Description = "template description here." %>
2. The entity class, data connection, and Table Name of the database to be generated are indispensable. In Codesmith Passed Schemaexplorer. dll To help you deal with databases.
<% @ Assembly name = "schemaexplorer" %>
<% @ Import namespace = "schemaexplorer" %>
Import in Template Schemaexplorer .
Set a series of parameters to be passed in:
<% @ Property name = "Database" type = "schemaexplorer. databaseschema" Category = "context" Description =" Database Connection . "%>
<% @ Property name = "tablename" type = "system. String" default = "" Optional = "false" Category = "" Description =" Table Name "%>
3. codesmithTemplate script format:
<SCRIPT runat = "template">
Codetemplaterule rule = new codetemplaterule ();
</SCRIPT>
Or:
/// <Summary>
/// Function : <% = Description %>
/// Author : <% = Author %>
/// Date : <% = Datetime. Now. tostring () %>
/// </Summary>
4.I wroteDLLTo store your own functions. Codetemplaterule. dll, Which referencesSchemaexplorer, For example:
Public Columnschemacollection getcolumncollection (databaseschema database, String Tablename)
{
Tableschemacollection tables = New Tableschemacollection (database. Tables );
Columnschemacollection Columns = Null ;
For ( Int I = 0 ; I < Tables. Count; I ++ )
{
If (Tables [I]. Name. toupper () = Tablename. toupper ())
{
Tableschema TS=Tables [I];
Columns=NewColumnschemacollection (TS. columns );
}
}
Return Columns;
}
The meaning of this Code is to take the set of all columns in a table in the database.
See Demo File:
Template file: Entity. CST
Self-written . Net ProgramSet: Codetemplaterule. CS
Effect of the generated code:Accountbookentity. CS
Iii. Running
1. Use Codesmith.exe Running template, Codesmith A dialog box is displayed to fill in your parameters.
2. Use Codesmithconsole.exe Run the template. The parameters can be placed in XML File. For example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Codesmith>
<Propertysets>
<Propertyset>
<Property name = "samplestringproperty1"> string111111111 </property>
<Property name = "samplebooleanproperty1"> false </property>
</Propertyset>
</Propertysets>
</Codesmith>
Run the following command:
3.InCodesmithstudio.exeRun Template
Summary:
Code generators bring great convenience to our programming work and do not need to do a lot of repetitive work.