Search code examples
c#.nettemplatescode-generationt4

Is T4 template run every time it is requested?


I am trying to setup an environment which would let me use a template to send out email to thousands of people. In all of this, performance is a key and I am trying to find out, when myscript.tt is run, is it compiled and executed every time or is there a compiled object that can be reused for subsequent calls to execute the script?


Solution

  • Basically, you call this in a pre-compiled template:

    string result = new MyTemplate().TransformText();
    

    The template and its code (logic, etc) is created once but you then call the TransformText to generate the dynamically desired output. In your case I think you feed your template with different e-mail addresses each time you call TransformText.

    This post will answer your question:

    Run-Time Text Generation with T4 Text Templates

    More details here:

    Writing Code that Writes Code