这里写的东东都是从CodeSmith自带的帮助文档中FAQ里学到的东东
1.如何在模板中添加注释
CodeSmith:
<%-- Comments --%>
VB.NET:
<%-- 'Comments --%>
C#:
<%-- // Comments --%>
<%-- /* Comments */ --%>
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>
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>
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;}
}
{
get {return _outputDirectory;}
set {_outputDirectory= value;}
}
7.怎样调用子模板
1<%
2foreach (TableSchema table in SourceDatabase.Tables)
3{
4 OutputSubTemplate(table);
5}
6%>
7<script runat="template">
8private CodeTemplate _mySubTemplate;
9
10[Browsable(false)]
11public CodeTemplate MySubTemplate
12{
13 get
14 {
15 if (_mySubTemplate == null)
16 {
17 CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + "MySubTemplate.cst");
18 compiler.Compile();
19 if (compiler.Errors.Count == 0)
20 {
21 _mySubTemplate = compiler.CreateInstance();
22 }
23 else
24 {
25 for (int i = 0; i < compiler.Errors.Count; i++)
26 {
27 Response.WriteLine(compiler.Errors[ i].ToString());
28 }
29 }
30 }
31 return _mySubTemplate;
32 }
33}
34
35public void OutputSubTemplate(TableSchema table)
36{
37 MySubTemplate.SetProperty("SourceTable", table);
38 MySubTemplate.SetProperty("IncludeDrop", false);
39 MySubTemplate.SetProperty("InsertPrefix", "Insert");
40 MySubTemplate.Render(Response);
41}
42</script>
2foreach (TableSchema table in SourceDatabase.Tables)
3{
4 OutputSubTemplate(table);
5}
6%>
7<script runat="template">
8private CodeTemplate _mySubTemplate;
9
10[Browsable(false)]
11public CodeTemplate MySubTemplate
12{
13 get
14 {
15 if (_mySubTemplate == null)
16 {
17 CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + "MySubTemplate.cst");
18 compiler.Compile();
19 if (compiler.Errors.Count == 0)
20 {
21 _mySubTemplate = compiler.CreateInstance();
22 }
23 else
24 {
25 for (int i = 0; i < compiler.Errors.Count; i++)
26 {
27 Response.WriteLine(compiler.Errors[ i].ToString());
28 }
29 }
30 }
31 return _mySubTemplate;
32 }
33}
34
35public void OutputSubTemplate(TableSchema table)
36{
37 MySubTemplate.SetProperty("SourceTable", table);
38 MySubTemplate.SetProperty("IncludeDrop", false);
39 MySubTemplate.SetProperty("InsertPrefix", "Insert");
40 MySubTemplate.Render(Response);
41}
42</script>
FAQ中给出的例子为生成一个数据库中所有表的更新Update存储过程
SubTemplatesExample.cst文件源代码
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure." %>
3
4<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
5 Category="Context"
6 Description="Database" %>
7
8<%@ Assembly Name="SchemaExplorer" %>
9
10<%@ Import Namespace="SchemaExplorer" %>
11
12<%
13foreach (TableSchema table in SourceDatabase.Tables)
14{
15 OutputSubTemplate(table);
16}
17%>
18
19<script runat="template">
20private CodeTemplate _mySubTemplate;
21
22
23
24
25[Browsable(false)]
26public CodeTemplate MySubTemplate
27{
28 get
29 {
30 if (_mySubTemplate == null)
31 {
32 CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + "MySubTemplate.cst");
33 compiler.Compile();
34
35 if (compiler.Errors.Count == 0)
36 {
37 _mySubTemplate = compiler.CreateInstance();
38 }
39 else
40 {
41 for (int i = 0; i < compiler.Errors.Count; i++)
42 {
43 Response.WriteLine(compiler.Errors[ i].ToString());
44 }
45 }
46 }
47
48 return _mySubTemplate;
49 }
50}
51
52public void OutputSubTemplate(TableSchema table)
53{
54 MySubTemplate.SetProperty("SourceTable", table);
55 MySubTemplate.SetProperty("IncludeDrop", false);
56 MySubTemplate.SetProperty("InsertPrefix", "Insert");
57
58 MySubTemplate.Render(Response);
59}
60</script>
2 Description="Generates a update stored procedure." %>
3
4<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
5 Category="Context"
6 Description="Database" %>
7
8<%@ Assembly Name="SchemaExplorer" %>
9
10<%@ Import Namespace="SchemaExplorer" %>
11
12<%
13foreach (TableSchema table in SourceDatabase.Tables)
14{
15 OutputSubTemplate(table);
16}
17%>
18
19<script runat="template">
20private CodeTemplate _mySubTemplate;
21
22
23
24
25[Browsable(false)]
26public CodeTemplate MySubTemplate
27{
28 get
29 {
30 if (_mySubTemplate == null)
31 {
32 CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + "MySubTemplate.cst");
33 compiler.Compile();
34
35 if (compiler.Errors.Count == 0)
36 {
37 _mySubTemplate = compiler.CreateInstance();
38 }
39 else
40 {
41 for (int i = 0; i < compiler.Errors.Count; i++)
42 {
43 Response.WriteLine(compiler.Errors[ i].ToString());
44 }
45 }
46 }
47
48 return _mySubTemplate;
49 }
50}
51
52public void OutputSubTemplate(TableSchema table)
53{
54 MySubTemplate.SetProperty("SourceTable", table);
55 MySubTemplate.SetProperty("IncludeDrop", false);
56 MySubTemplate.SetProperty("InsertPrefix", "Insert");
57
58 MySubTemplate.Render(Response);
59}
60</script>
MySubTemplate.cst文件源代码
1<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL"
2 Description="Generates a update stored procedure." %>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on." %>
7
8<%@ Assembly Name="SchemaExplorer" %>
9
10<%@ Import Namespace="SchemaExplorer" %>
11
12
13<script runat="template">
14public string GetSqlParameterStatement(ColumnSchema column)
15{
16 string param = "@" + column.Name + "
2 Description="Generates a update stored procedure." %>
3
4<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"
5 Category="Context"
6 Description="Table that the stored procedures should be based on." %>
7
8<%@ Assembly Name="SchemaExplorer" %>
9
10<%@ Import Namespace="SchemaExplorer" %>
11
12
13<script runat="template">
14public string GetSqlParameterStatement(ColumnSchema column)
15{
16 string param = "@" + column.Name + "