views:

303

answers:

1

I'm trying to use a class within a T4 template in VS2008.

Here is a simplified version of what I'm doing...

<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".vb" debug="True" hostspecific="True" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Windows.Forms.dll" #>
<#@ assembly name="System.xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SQLClient" #>
<# Call (New SomeClass).Start()#>
<#+
Private Class SomeClass
    Public Sub Start()
    #>test<#+
    End Sub
End Class 
#>

When I run this template... I get the following error...


Error 1 Compiling transformation: 'Write' is not a member of 'Microsoft.VisualStudio.TextTemplatingF77BDE667ECAD297F587D3D651053846. GeneratedTextTransformation.SomeClass'. D:\Development\PrivateProjects\CodeGeneration\CodeGeneration\Generation\Common\test2.tt 16 1 CodeGeneration

Can anyone tell my why this causes the error it does and more importantly how to negate it's effects?

+1  A: 

The code...

#>test<#+

... internally translates to...

Write("test");

Since my class does not have a 'Write' method, the compile fails.

The workaround is....

<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".vb" debug="True" hostspecific="True" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Windows.Forms.dll" #>
<#@ assembly name="System.xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Data.SQLClient" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>

<# Call (New SomeClass(Me)).Start()#>

<#+
Private Class SomeClass
Private mOutput as TextTransformation
Public Sub New(Output as TextTransformation)
 mOutput = Output
End Sub 
Public Sub Write(SomeText as String)
 mOutput.Write(SomeText)
End Sub 
    Public Sub Start()
        #>test<#+
    End Sub
End Class 
#>

Which results in the write being passed to the parent class for processing.

Rory Becker