CodeSmith用子模版的RenderToFile输出到指定文件

    CodeSmith是一款不错的.Net开发辅助工具,可以使工作效率得到很大提高。最近用它做了一项目,其中要自动保存多个文件。上网查了查,能查到的都是用继承 Inherits = " OutputFileCodeTemplate " 方法来实现,个人觉得这方法过于麻烦。后来查了查CodeSmith 4.1的联机帮助,里面讲得很清楚,用子模版的RenderToFile就可以了,原文如下:
This technique is useful for generating multiple identical copies of the same file. When you need to generate multiple different files as part of a single code-generation process, you should use one sub-template for each file. Call the sub-templates from a master template and use the RenderToFile method to output each sub-template.
以下是我写的Demo,很简单吧:
 1 public   void  RenderMyAspxCs(TableSchema table, string  strTableName, string  nameSpaceStr) 
 2
 3    CodeTemplate subTemplate=null;
 4    subTemplate=CreateSubTemplate(@"..\Web.UI\MyAspx.cs.cst"); //创建template
 5    if(subTemplate!=null)
 6    {
 7        subTemplate.SetProperty("CurTable", table); 
 8        subTemplate.SetProperty("NameSpaces""CommunityOA.Web.Module.OA."+nameSpaceStr); 
 9        subTemplate.SetProperty("EntityName", strTableName); 
10        subTemplate.SetProperty("CodeEntity",strTableName); 
11       
13        subTemplate.RenderToFile(FoldName+"\\"+nameSpaceStr+"\\My.Aspx.cs",true); //输出到文件
14    //    Response.WriteLine(FoldName+"\\"+nameSpaceStr+"\\Manager.Aspx.cs"); //得示信息
15    }
 
16}
 
17
18 public  CodeTemplate CreateSubTemplate( string  SubTempName) 
19
20        CodeTemplate _SubTemplate=null;  //定义子模块
21         CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName + SubTempName); 
22         compiler.Compile(); 
23         
24         if (compiler.Errors.Count == 0
25         
26            _SubTemplate = compiler.CreateInstance(); 
27         }
 
28         else 
29         
30            for (int i = 0; i < compiler.Errors.Count; i++
31            {
32               Response.WriteLine(compiler.Errors[ i].ToString()); 
33            }
 
34         }
 
35      
36      return _SubTemplate; 
37}
 
38

转载于:https://www.cnblogs.com/yuanbao/archive/2007/12/26/1015598.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值