这里写的东东都是从CodeSmith自带的帮助文档中FAQ里学到的东东CodeSmith基础(三)
        1.如何在模板中添加注释
        CodeSmith:
        <%-- Comments --%>
        VB.NET:
        <%-- 'Comments --%>
        C#:
        <%-- // Comments --%>
        <%-- /* Comments */ --%>

        2.创建一个可以下拉选择的属性
        首先定义一个枚举类型的变量,然后将属性的类型设置为枚举型
 1 <%@ Property Name="CollectionType" Type="CollectionTypeEnum" Category="Collection" Description="Type of collection" %>
 2 
 3 <script runat="tempate">
 4 public enum CollectionTypeEnum
 5 {
 6     Vector,
 7     HashTable,
 8     SortedList
 9 }
10 </script>

        3.解决ASP.NET中标签<%重复问题
        先将ASP.NET中使用的这个重复标签写成<%%,避免在生成代码时由于是标签重复引起的编译错误或生成错误。

        4.如何声明一个常量
       
<script runat="template">
private const string MY_CONST 
= "example"
</script>

        5.如何对模板进行调试
        如果要调试一个模板,首先要在代码模板里进行声明,然后在你想要进行调试的地方用Debugger.Break()语句设置断点即可。
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Debugging your template" Debug="true" %>

<% Debugger.Break(); %>

        6.如何将属性设置成选择一个文件夹的路径
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]

public string OutputDirectory
{
      
get {return _outputDirectory;}
      
set {_outputDirectory= value;}
}

        7.怎样调用子模板
 1CodeSmith基础(三)<% 
 2CodeSmith基础(三)foreach (TableSchema table in SourceDatabase.Tables) 
 3>
        FAQ中给出的例子为生成一个数据库中所有表的更新Update存储过程
        SubTemplatesExample.cst文件源代码
 1CodeSmith基础(三)<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2CodeSmith基础(三)    Description="Generates a update stored procedure." %>
 3CodeSmith基础(三)
 4CodeSmith基础(三)<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
 5CodeSmith基础(三)    Category="Context"
 6CodeSmith基础(三)    Description="Database" %>
 7CodeSmith基础(三)
 8CodeSmith基础(三)<%@ Assembly Name="SchemaExplorer" %>
 9CodeSmith基础(三)
10CodeSmith基础(三)<%@ Import Namespace="SchemaExplorer" %>
11CodeSmith基础(三)
12CodeSmith基础(三)<% 
13CodeSmith基础(三)foreach (TableSchema table in SourceDatabase.Tables) 
14>
        MySubTemplate.cst文件源代码
 1CodeSmith基础(三)<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2CodeSmith基础(三)    Description="Generates a update stored procedure." %>
 3CodeSmith基础(三)
 4CodeSmith基础(三)<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 5CodeSmith基础(三)    Category="Context"
 6CodeSmith基础(三)    Description="Table that the stored procedures should be based on." %>
 7CodeSmith基础(三)
 8CodeSmith基础(三)<%@ Assembly Name="SchemaExplorer" %>
 9CodeSmith基础(三)
10CodeSmith基础(三)<%@ Import Namespace="SchemaExplorer" %>
11CodeSmith基础(三)    
12CodeSmith基础(三)    
13CodeSmith基础(三)<script runat="template">
14CodeSmith基础(三)public string GetSqlParameterStatement(ColumnSchema column)
15%>

        8.在加载模板时默认加载的命名空间Namespaces和组件Assemblies
        组件:mscorlib, System, System.Xml, System.Data, System.Drawing, Microsoft.VisualBasic, System.Windows.Forms, CodeSmith.Engine
        命名空间:System, System.Data, System.Diagnostics, System.ComponentModel, Microsoft.VisualBasic, CodeSmith.Engine

        9.使用SchemaExplorer能否确定一个字段(Field)是标识字段(主键,Identity Field)
        在字段的扩展属性集合中包含一个叫“CS_IsIdentity”的属性,如果这个属性的值为true,则表名当前字段为一个标识字段
1 Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {  
2       if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true)
3       {
4             Response.Write(cs.Name);
5       }
6 }
7 %>
        CS_Identity_Example.cst文件源代码
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2     Description="Identifies the identity field of a table" %>
 3 
 4 <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
 5     Category="Context"
 6     Description="Table to target." %>
 7 
 8 <%@ Assembly Name="SchemaExplorer" %>
 9 
10 <%@ Import Namespace="SchemaExplorer" %>
11 
12 
13 
14 Identity Field = <% foreach(ColumnSchema cs in SourceTable.Columns) {  
15                         if( ((bool)cs.ExtendedProperties["CS_IsIdentity"].Value) == true)
16                         {
17                             Response.Write(cs.Name);
18                         }
19                     }
20                  %>

        10.如何确定一个字段的默认值(各人认为翻译成如何知道一个字段有默认值并且默认值是什么)
        在字段的扩展属性集合中包含一个叫“CS_Default”的属性

1 <%
2 foreach(ColumnSchema cs in SourceTable.Columns) {  
3       if (cs.ExtendedProperties["CS_Default"!= null)
4       {
5            Response.WriteLine(cs.ExtendedProperties["CS_Default"].Value);
6       }
7 }
8 %>

        11.如何使用SchemaExplorer得到存储过程的输入输出参数
        使用CodeSmith提供的CommandSchema对象,它包含需要的输入输出参数集合
 1 Input Parameters: 
 2 <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
 3 {
 4       Response.Write(ps.Name);
 5       Response.Write("\n");
 6 }
 7 %>
 8 
 9 
10 Output Parameters: 
11 <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
12 {
13       Response.Write(ps.Name);
14       Response.Write("\n");
15 }
16 %>
        InputOutputParameterExample.cst文件源代码
 1 <%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
 2     Description="Generates a update stored procedure." %>
 3 
 4 <%@ Property Name="SourceProcedure" Type="SchemaExplorer.CommandSchema"
 5     Category="Context"
 6     Description="The stored procedure to examine" %>
 7 
 8 <%@ Assembly Name="SchemaExplorer" %>
 9 
10 <%@ Import Namespace="SchemaExplorer" %>
11 
12 Input Parameters: 
13 <%foreach(ParameterSchema ps in SourceProcedure.AllInputParameters)
14 {
15     Response.Write(ps.Name);
16     Response.Write("\n");
17 }
18 %>
19 
20 
21 Output Parameters: 
22 <%foreach(ParameterSchema ps in SourceProcedure.AllOutputParameters)
23 {
24     Response.Write(ps.Name);
25     Response.Write("\n");
26 }
27 %>

相关文章: