T4 Text Templating

903 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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:

<#+ #>