Is there any way to have functions in basic T4 templates?

Patrick Karcher picture Patrick Karcher · Feb 4, 2010 · Viewed 11.3k times · Source

By basic T4 template, I mean not using T4 Toolkit or any of the add-ins.

My T4 is getting a little complicated, but I'd like to keep in self-contained for now. Is there a way have functions in your T4 template, without referencing external assemblies?

Answer

Mitch Wheat picture Mitch Wheat · Feb 4, 2010

You mean like this:

<#+
    public List<string> Dostuff()
    {
        List<string> result = new List<string>(); 

        // ...

        return result;
    }
#>

Here's a complete example: Reading a Xml File in T4 Templates

Oleg Sych's T4Toolbox is a good resource.