CodeSmith实用技巧(七):从父模版拷贝属性

 在使用CodeSmith进行代码生成的时候,你可能需要在子模版和父模版之间共享属性。比如,写一个基于数据库生成代码的模版,在每个模版里面都定义了一个名为Server的属性。当你在父模版中使用此属性时,它的值只对父模版起作用。想要设置此值到子模版,可以在父模版中使用CopyPropertiesTo方法,当在父模版中使用此属性时,它的值会发送到子模版中去。下面这段代码展示了如何使用该方法:
  // instantiate the sub-template
   Header header = new Header();
   
// copy all properties with matching name and type to the sub-template instance
   this.CopyPropertiesTo(header);
posted @ 2005-12-28 08:42  TerryLee  阅读(3894)  评论(3编辑  收藏  举报