I’m starting to investigate T4 for Code Generation.
I get that you have a basic template in which you can embed little chunks of c#/vb which can perform clever stuff…
<#@ template language='VB' debug='True' hostspecific='True' #> <#@ output extension='.vb' debug='True' hostspecific='True' #> Imports System <#For Each Table as String in New String(0 {'Table1','Table2'}#> Public Class <#=Table#>DA Public Sub New <#= WriteConstructorBody() #> End Sub End Class <#Next#> <#+ Public Function WriteConstructorBody() as String return '' Some comment' End function #>
This is great.. However I would like to be able to write my main block thus…
<#@ template language='VB' debug='True' hostspecific='True' #> <#@ output extension='.vb' debug='True' hostspecific='True' #> Imports System <# For Each BaseTableName as String in New String(){'Table1','Table2'} WriteRecDataInterface(BaseTableName) WriteRecDataClass(BaseTableName) WriteDAInterface(BaseTableName) WriteDAClass(BaseTableName) Next #>
Then I would like to be able to have the WriteX methods exist in a Class Block but themselves be writable using code by example ie escaped Code blocks.
How can I achieve this?
It seems that you can mix static output with template code in Class Blocks. Here is an example with C#: