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

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.RenderToFileCodeTemplate.RenderToString方法可能更有用,它可以直接让结果输出到文件中或赋给字符型的变量。

注意:该功能只能在CodeSmith专业版中使用
作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2005-12-28 08:51 TerryLee 阅读(2165) 评论(2)  编辑 收藏 网摘 所属分类: [13]  代码生成

  回复  引用  查看    
#1楼2006-08-04 22:16 | 达达      
这好需要这个功能,感谢感谢!
  回复  引用  查看    
#2楼[楼主]2006-08-05 14:27 | TerryLee      
@达达

不用客气:-)




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 306236




相关文章:

相关链接: