好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

CodeSmith实用技巧(十):通过编程执行模版

CodeSmith 在执行模版时通过调用一些 API 来完成的,主要经过了以下这几步的操作: l 编译一个模版 l 显示编译错误信息 l 创建一个新的模版实例 l 用元数据填充模版 l 输出结果 下面这段代码显示了这些操作: CodeTemplateCompilercompiler = new CodeTempla

CodeSmith 在执行模版时通过调用一些 API 来完成的,主要经过了以下这几步的操作:

l 编译一个模版

l 显示编译错误信息

l 创建一个新的模版实例

l 用元数据填充模版

l 输出结果

下面这段代码显示了这些操作:

CodeTemplateCompiler compiler = new CodeTemplateCompiler( " ..\\..\\StoredProcedures.cst " );
compiler.Compile();

if (compiler.Errors.Count == 0 )
{
CodeTemplate template = compiler.CreateInstance();

DatabaseSchema database = new DatabaseSchema( new SqlSchemaProvider(), @" Server=(local)\NetSDK;Database=Northwind;Integrated Security=true; " );
TableSchema table = database.Tables[ " Customers " ];

template.SetProperty( " SourceTable " , table);
template.SetProperty( " IncludeDrop " , false );
template.SetProperty( " InsertPrefix " , " Insert " );

template.Render(Console.Out);
}
else
{
for ( int i = 0 ; i compiler.Errors.Count; i ++ )
{
Console.Error.WriteLine(compiler.Errors[i].ToString());
}
}

在这里我们用了 Render 方法,其实 CodeTemplate.RenderToFile 和 CodeTemplate.RenderToString 方法可能更有用,它可以直接让结果 输出到文件中或赋给字符型的变量。

注意: 该功能只能在 CodeSmith 专业版中使用

查看更多关于CodeSmith实用技巧(十):通过编程执行模版的详细内容...

  阅读:35次

上一篇: 改良程序必知必会的11个经典技巧

下一篇:squid简单配置文档