CodeSmith实用技巧(九):重载Render方法来控制输出
在CodeSmith中,CodeTemplate.Render方法是在模版执行完成进行模版输出时执行,你可以通过重载CodeTemplate.Render方法来修改CodeSmith输出时的事件处理。例如:你可以修改模版输出时的方式来代替现在默认的方式,下面这段代码展示了在保持CodeSmith默认的窗口显示的同时,把结果输出到两个不同的文件。
注意不能忘了base.Render(writer);这句话,否则你将不能获得默认的输出。当重载CodeTemplate.Render方法时,你也可
以访问TextWriter,也就是说你也可以直接添加其它的附属信息到模版输出的内容中。
1
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="AddTextWriter Demonstration." %>
2
<%@ Import Namespace="System.IO" %>
3
This template demonstrates using the AddTextWriter method
4
to output the template results to multiple locations concurrently.
5
<script runat="template">
6
public override void Render(TextWriter writer)
7
{
8
StreamWriter fileWriter1 = new StreamWriter(@"C:\test1.txt", true);
9
this.Response.AddTextWriter(fileWriter1);
10
11
StreamWriter fileWriter2 = new StreamWriter(@"C:\test2.txt", true);
12
this.Response.AddTextWriter(fileWriter2);
13
14
base.Render(writer);
15
16
fileWriter1.Close();
17
fileWriter2.Close();
18
}
19
</script>
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
19
![](/Images/OutliningIndicators/None.gif)
以访问TextWriter,也就是说你也可以直接添加其它的附属信息到模版输出的内容中。
支持TerryLee的创业产品Worktile
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com