3

I have the following .tt file that I wish to use as an include file where I want to expose some properties to the main T4 files:

Include.tt

<#@ assembly name="$(ProjectDir)bin\Debug\EPPlus.dll" #>
<#@ assembly name="System.Configuration.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Configuration" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="OfficeOpenXml" #>
<#
    public static string EDIInputPath
    {
        get
        {
            return ConfigurationManager.AppSettings["inputPath"];
        }
    }
#>

When saving this code it throws 7 compile time errors with the first one saying "Statement expected".

I don't use T4 templates very often so I am not entirely sure what I am doing wrong here.

Intrepid
  • 2,781
  • 2
  • 29
  • 54
  • You may already be doing this but if you are not using Tangible T4 or some other extension to make t4 more readable then definitely grab it! http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html it makes working with them so much easier. You can also right click the TT file and "Debug Template" – Tony Jun 20 '14 at 12:26

1 Answers1

2

The easiest way to see what is going wrong is to change the CustomTool property on the T4 file to be TextTemplatingFilePreprocessor. Then you can see the generated code inside Visual Studio.

In your case the problem is that inside the standard control block

<# #> 

you can only have statements that would work inside a method. The T4 template engine will put these statements inside a TransformText() method that outputs the text for the template.

You have defined a property which is not allowed inside a method. Your property code needs to go into a class feature block:

<#+ #>
Matt Ward
  • 47,057
  • 5
  • 93
  • 94