Is there any way to have functions in basic T4 tem

2019-04-17 18:37发布

问题:

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?

回答1:

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.