您好,欢迎访问一九零五行业门户网

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专业版中使用
其它类似信息

推荐信息